I need this in Python please! Below is a recursive program that finds the larges
ID: 3697833 • Letter: I
Question
I need this in Python please!
Below is a recursive program that finds the largest element in a list.
def largest(data) :
if len(data) == 1 :
return data[0]
largestRest = largest(data[0 : -1])
if largestRest < data[-1] :
return data[-1]
else :
return largestRest
def main() :
# Demonstrate the largest function.
print(largest([10, 12, 33, 8, 52, 49, 23, 14, 1]))
print("Expected: 52")
# Call the main function.
main()
Write a recursive program that finds the smallest element in a list. (include print statements that show the trace of the variables changing)
Explanation / Answer
def smallest(data) :
if len(data) == 1 :
return data[0]
smallestRest = smallest(data[0 : -1])
if smallestRest > data[-1] :
print "smallestRest : ",smallestRest
return data[-1]
else :
return smallestRest
def main() :
# Demonstrate the largest function.
print(smallest([10, 12, 33, 8, 52, 49, 23, 14, 1]))
print("Expected : 1")
# Call the main function.
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.