Question: 18b. load_regular_grid.py Write a function load_regular_grid(filename) that reads a space-delimited file of items into a grid. If the grid is jagged, return None . For

18b. load_regular_grid.py

Write a function load_regular_grid(filename) that reads a space-delimited file of items into a grid. If the grid is jagged, return None.

For example, if grid1.txt contains:

1 2 3 4 5 6

then

load_regular_grid("grid1.txt")

should return [[1, 2, 3], [4, 5, 6]].

On the other hand, if a file, such as grid.txt contains:

grid2.txt

1 2 3 4 5

then

load_regular_grid("grid2.txt")

should return None.

18c. flatten_grid.py

Write a function flatten_grid(grid) that flattens the rows of grid into one list.

For example:

flatten_grid([[3, 1, 4], [1, 5, 9]])

should return [3, 1, 4, 1, 5, 9].

18d. print_with_headers.py

Write a function print_with_headers(grid) where grid only contains single-digit integers. The function will print out the grid, along with row and column headers. Each header should start with 0.

For example:

print_with_headers([[3, 1, 4], [1, 5, 9]])

should print:

 0 1 2 0 3 1 4 1 1 5 9

Notice that 0 1 2 along the top means that these are columns 0, 1, and 2 in the grid. Likewise, 0 1 along the left hand side means these are rows 0 and 1.

The central part:

3 1 4 1 5 9

is the rows and columns of the grid. The challenge here is to know how to print the first line of output the column labels based on the size of the grid. Then, as you print out rows, keep track of the row number so you can start each row with its label.

18e. reshape_grid.py

Write a function reshape_grid(items, num_rows, num_columns)that returns a grid built from items with designated num_rows and num_colimns. If the number of items doesnt fill up grid, set the remaining slots to None.

For example, if you call this function as follows:

reshape_grid(["A1", "B1", "C1", "D1", "A2", "B2", "C2", "D2", "A3"], 3, 4)

this return:

[['A1', 'B1', 'C1', 'D1'], ['A2', 'B2', 'C2', 'D2'], ['A3', None, None, None]]

Notice that this function creates a new grid, and then fills it with the supplied items, in order, filling in each row. Once all of the items in items are used up, then every other entry in the new grid is set to None.

18f. grid_compare.py

Write a function grid_compare(grid1, grid2) where grid1 and grid2 are grids of the same size. The function should return a new grid that is True where the elements match and is False when they dont.

For example:

grid_compare([[1, 2], [2, 1]], [[1, 1], [2, 2]])

should return:

[[True, False], [True, False]]

Notice that we are comparing:

1 2 2 1

To

1 1 2 2

The grid_compare() function looks at row 0, item 0, and compares the two items in the same location in each grid. It then moves on to row 0, item 1, and so forth.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!