140k views
3 votes
Guidelines:

 Loops are completely banned (for loop and while loop)
 You must use recursion in each function
 You are not allowed to import anything.
 You are not allowed to use the built-in function max(). Everything else is okay
 You are allowed to use sets, dictionaries, and any of their respective operations.
 You are allowed to use any string method except for string.join()
 You can do string slicing, but you cannot use the string[::-1] shortcut.
 You can use any list operation, except for list.sort() and list.reverse()
 Do not hard code to the examples
------------------------------------------------------------------------------------------------------------
Functions (You must use recursion in each function WITHOUT altering the original function signature):
def merge(listA, listB):
Description:
Combine two lists into one list, maintaining the order of the elements. You should assume that both lists given to this function contain elements in ascending order (smallest first).
Parameters:
listA, listB, two lists of elements (could be anything – assume homogenous data)
Return value:
a list, the combination of elements from listA and listB in ascending order.
Examples:
merge([1,2,3], [4,5,6]) → [1,2,3,4,5,6]
merge([1,2,3], [2,3,4]) → [1,2,2,3,3,4]
merge([2,4,6], [1,3,5]) → [1,2,3,4,5,6]
---------------------------------------------------
def largest_sum(xs, x, y):

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 pattern 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

User Briarheart
by
5.3k points

1 Answer

5 votes

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))

User Ravi Mariya
by
5.7k points