Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

python recursive Define a merge function, whose first argument is a list (possib

ID: 3835809 • Letter: P

Question

python recursive

Define a merge function, whose first argument is a list (possibly empty), always containing equal-length strings; its second argument is a single string. It returns a list, always containing equallength strings, that contains all the longest strings from the first and second arguments. For example merge([],'abc’) returns ['abc’] merge(['abc’, 'lmn’],'wxyz’) returns ['wxyz’] merge(['abc’, 'lmn’],'xyz’) returns ['abc’, 'lmn’, 'xyz’] merge(['abc’, 'lmn’],'xy’) returns ['abc’, 'lmn’] Write the merge function using the functional style, with no mutation of lists (e.g., no append).

def merge(x : [str], y : str) -> [str]:

Explanation / Answer

def merge(str_list, string):
if not str_list:
return [string]
if len(string) < len(str_list[0]):
return str_list
elif len(string) > len(str_list[0]):
return [string]
else:
return str_list + [string]

print(merge(['abc', 'lmn'],'xyz'))
print(merge([],'abc'))
print(merge(['abc', 'lmn'],'wxyz'))
print(merge(['abc', 'lmn'],'xy'))

# code link: https://paste.ee/p/n4YEC