Question
1.Python 2-dimensional List: 1a. Write a Python function copy that takes a 2-dimensional list and returns a (deep) copy of it. First make sure you
1.Python 2-dimensional List:
1a. Write a Python function copy that takes a 2-dimensional list and returns a (deep) copy of it. First make sure you understand why the following code doesnt work:
def copy(x):
return [x[i] for i in range(len(x))]
Use slicing to help make the copy.
1b. Write a Python function odd_cols that takes a rectangular 2-dimensional list and returns a copy of all the odd-index columns as a new 2-dimensional list. For example,
odd_cols([[3, 5, 2, 8, 9],
[1, 0, 3, 7, 2],
[4, 8, 9, 3, 6]])
should return
[[5, 8],
[0, 7],
[8, 3]]
1c. Write a Python function make_rect that takes a 2-dimensional list thats not necessarily rectangular, and modifies it to be rectangular (with width equal to the maximum width of the original rows) by appending None values to the rows that are too short. For example,
x = [[0, 1],
[2, 3, 4, 5, 6],
[7, 8, 9]]
make_rect(x)
should not return anything (which is equivalent to returning None), but after the call, x should be
[[0, 1, None, None, None],
[2, 3, 4, 5, 6],
[7, 8, 9, None, None]]
Do not use Pythons built-in max function; find the maximum row length using a loop (as discussed in lecture). (You may wish to use the extend method of lists: if a and b are lists, then a.extend(b) appends a copy of all entries of b onto the end of a.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started