How can I Write a function called largest that takes a list of integers as argum
ID: 3819674 • Letter: H
Question
How can I Write a function called largest that takes a list of integers as argument. It Recursively returns the largest integer of the list. Name of the file: largest.py (Python). I am having issues understanding how to add a recursive into the function, help also with the recursive.
Here are few sample runs given:
>>> largest( [1,12,4,10])
12
>>> Largest([])
Traceback (most recent call last):
File …..
print(Largest([]))
File …..
raise ValueError('Please provide a non empty list')
ValueError: Please provide a non empty list
>>>Largest([1,2.2])
Traceback (most recent call last):
File xxxxx
print(Largest([1,2.2]))
File xxxxx
raise TypeError('the passed argument is not an array')
TypeError: the passed argument is not an array
Explanation / Answer
def largest(lt):
if len(lt)>0:
a = lt[0]
b = largest(lt[1:])
if(a>b):
return a
return b
print(largest([1,12,4,10]))
print(largest([]))
print(largest([1,2.2]))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.