Write a function definition, max_absolute , that takes as arguments 0 or more nu
ID: 3909499 • Letter: W
Question
Write a function definition, max_absolute, that takes as arguments 0 or more numbers, and returns the number with the highest absolute value. If no numbers are specified, the function must return the Python constant None - not the string "None".
You may assume that the arguments passed to the function, if any, are all valid numbers. The function must be able to take an arbitrary number of arguments.
You may test your function as follows:
max_absolute(-100, 2.5, 70) should return -100.
max_absolute(-10) should return -10.
max_absolute() should return the Python constant None (not the string "None")
max_absolute(3, -100, 2, 70, 100, -2, -70) can return either -100 or 100.
Please note that the function must return a result, not print it.
Hint: The built-in Python function abs returns the absolute value of a number.
Explanation / Answer
def max_absolute(*args): max_val = None for arg in args: if max_val is None or abs(arg) > abs(max_val): max_val = arg return max_val print(max_absolute(-100, 2.5, 70)) print(max_absolute(-10)) print(max_absolute()) print(max_absolute(3, -100, 2, 70, 100, -2, -70))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.