In Python, The maximum-valued element in a tuple can be recursively calculated a
ID: 3763804 • Letter: I
Question
In Python,
The maximum-valued element in a tuple can be recursively calculated as follows:
If the tuple has a single element, that is its maximum (tuples of size zero have no maxiumum)
Otherwise, compare the first element with the maximum of the rest of the tuple
Write a function named max that accepts a tuple and returns the largest value in the tuple. Assume the tuple has at least one element.
Explanation / Answer
def Max(list): if len(list) == 1: return list[0] else: m = Max(list[1:]) return m if m > list[0] else list[0] def main(): list = eval(raw_input(" please enter a list of numbers: ")) print("the largest number is: ", Max(list)) main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.