Question: def fill_matrix (fh_in, rows, cols): ------------------------------------------------------- Creates a rows by cols 2D list of integers filled with space-separated integers from f_in. If f_in does
def fill_matrix(fh_in, rows, cols):
"""
-------------------------------------------------------
Creates a rows by cols 2D list of integers filled with
space-separated integers from f_in. If f_in does not have enough values,
fill the remaining slots with 0s. If f_in has too many values,
the excess values are ignored.
Use: matrix = fill_matrix(fh_in, rows, cols)
-------------------------------------------------------
Parameters:
fh_in - the integers file to process (file handle - already open for reading)
rows - rows in matrix (int > 0)
cols - columns in matrix (int > 0)
Returns:
matrix - a 2D list of integers (2D list of int)
-------------------------------------------------------
"""
# Your code here
Complete the function fill_matrix in the t05_functions.py module. The module t05.py provides simple testing for this function.
Given the number of rows and columns as parameters, this function fills a matrix with integers from a file. If the file does not have enough values, the remaining slots are filled with 0s. If the file has too many values, the excess values are ignored.
The sample file numbers.txt contains:
2 3 1 6 23 12 44 56 78 21 45 23 67 54 2 1 7 7 7 8 5 5 3 66 8 23 12
Some examples from executing t05.py against numbers.txt:
fill_matrix(f_in, 2, 2) -> [[2, 3], [1, 6]] fill_matrix(f_in, 5, 3) -> [[2, 3, 1], [6, 23, 12], [44, 56, 78], [21, 45, 23], [67, 54, 2]]
Requirements
This function must:
- Not open or close any files - the testing does that
- Return, not print, the resulting 2D list
return
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
