Answer:
See Explaination
Step-by-step explanation:
def merge(listA, listB):
if not listA:
return listB
if not listB:
return listA
# create a empty resulting list(merged list)
result = []
# check the first elements of list listA, listB)
if(listA[0]<listB[0]):
# append the first element of listA to result
result.append(listA[0])
# use recursion to get remaning elements
# listA is now reduced to listA[1:]
result.extend(merge(listA[1:],listB))
else:
# append the first element of listB to result
result.append(listB[0])
# use recursion to get remaning elements
# listB is now reduced to listB[1:]
result.extend(merge(listA,listB[1:]))
# return the resultant list
return result
def largest_sum(xs, x, y):
# get number of rows and columns
m = len(xs[0])
n = len(xs)
# if move is invalid
if(x>=n or y>=m):
return 0
# check if we have reached boundary then return xs[x][y]
if(x==(n-1) and y==(m-1)):
return xs[x][y]
else:
# return max of two possible moves(leftmove, down move)
# move left (in this increment x)
left = xs[x][y] + largest_sum(xs,x+1,y)
# move down (in this increment y)
down = xs[x][y] + largest_sum(xs,x,y+1)
# return the maximum of two possible moves
if(left>down):
return left
else:
return down
# test code
print("Output for sample test cases given in problem:")
print(merge([1,2,3], [4,5,6]))
print(largest_sum([[1,2],[3,0]],0,0))
print(largest_sum([[5,6,1],[2,3,3]],0,0))
print(largest_sum([[0,7,5],[6,-1,4],[-5,5,2]],0,0))
print(largest_sum([[0,7,5],[6,-1,4],[-5,5,2]],1,1))