Reference no: EM132212318
Write a recursive function, max_multiples_list(lst, m), which takes an non-empty list, lst, of integers as a parameter and m is also an integer.
This function calculates and returns the largest value which is a multiple of m in the list. 0 is returned if there is no number that is the multiples of m in the list.
The base case will probably deal with the scenario where the list has just one value. The recursive case will probably call the function recursively using the original list, but with one item removed.
Test:
l = [1, 4, 5, 9, 11]
print(max_multiples_list(l, 3))
Result:
9
And there is my code, can anyone give a little help about how can I change my code?
def max_multiples_list(lst,m):
if len(lst)==1:
if lst[0] % m ==0:
return lst[0]
else:
return 0
else:
if lst[0]%m ==0:
return max(lst[0],max_multiples_list(lst[1:]))
else:
return 0