Question
Write a function increment(grid) that takes an existing 2-D list of digits and increments each digit by 1. If incrementing a given digit causes it
Write a function increment(grid) that takes an existing 2-D list of digits and increments each digit by 1. If incrementing a given digit causes it to become a 10 (i.e., if the original digit was a 9), then the new digit should wrap around and become a 0.
Important notes:
Unlike the other functions that you wrote for this problem, this function should notcreate and return a new 2-D list. Rather, it should modify the internals of the existing list.
Unlike the other functions that you wrote for this problem, this function should nothave a return statement, because it doesnt need one! Thats because its parametergrid gets a copy of the reference to the original 2-D list, and thus any changes that it makes to the internals of that list will still be visible after the function returns.
The loops in this function need to loop over all of the cells in the grid, not just the inner cells.
For example:
>>> grid = diagonal_grid(5, 5) >>> print_grid(grid) 10000 01000 00100 00010 00001 >>> increment(grid) >>> print_grid(grid) 21111 12111 11211 11121 11112 >>> increment(grid) >>> print_grid(grid) 32222 23222 22322 22232 22223 >>> grid = inner_grid(6, 4, 8) >>> print_grid(grid) 0000 0880 0880 0880 0880 0000 >>> increment(grid) >>> print_grid(grid) 1111 1991 1991 1991 1991 1111 >>> increment(grid) >>> print_grid(grid) 2222 2002 2002 2002 2002 2222
Heres another example that should help to reinforce your understanding of references:
>>> grid1 = inner_grid(5, 5, 1) >>> print_grid(grid1) 00000 01110 01110 01110 00000 >>> grid2 = grid1 >>> grid3 = grid1[:] >>> increment(grid1) >>> print_grid(grid1) 11111 12221 12221 12221 11111
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