Question
Description: Zig-zag through a two dimensional list of integers from some starting point until you reach one of the list's boundaries, computing the largest sum that you find along the way. X and Y represent the row and column position in xs of the first number to use in the sum. The zig-zag patten is made by limiting yourself to only looking at the number immediately on the right of (x,y) and the number immediately below (x,y) when figuring out which of those numbers yields the largest sum.
Parameters: xs, a 2D list of integers, x,y are the row and col position in xs Return value: an integer, the largest sum you can find from position (x,y)
Examples:
largest_sum([[1,2],[3,0]],0,0) → 4
largest_sum([[5,6,1],[2,3,3]],0,0) → 17
largest_sum([[0,7,5],[6,-1,4],[-5,5,2]],0,0) → 18
largest_sum([[0,7,5],[6,-1,4],[-5,5,2]],1,1) → 6
Solution Preview
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.
def largest_sum(xs, x, y):# dim_1 and dim_2 are numbers of rows and columns (boundaries)
dim_1 = len(xs)
dim_2 = len(xs[0])
# first we are resolving the case if x and y are less than boundaries
if x < dim_1 and y < dim_2:
left_move = xs[x][y] + largest_sum(xs, (x + 1), y) # left move where the x is increased...
By purchasing this solution you'll be able to access the following files:
Solution.py.