Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

18b. 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,

18b. 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

18c. 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

18d. 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

18e. 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

18f. 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

Give eye contact, but do not stare.

Answered: 1 week ago