+ 1

What solved this test ?

You are required to complete the function maximum(x). where "x" is a list of numbers, and contains more than 2 numbers. the function is expected to return the highest number in that list. Example: input : [5,20,12,6] output: 20 you can change the numbers in the list no_list but you are not allowed to change the variable names or edit any other code, except the function's body, doing so may jeopardize your evaluation These are instructions for solution In [ ]: no_list = [1,2,3,4]​def maximum(no_list): #complete the function to return the highest number in the list​print(maximum(no_list))

26th Apr 2021, 5:34 AM
Negm Abozed
Negm Abozed - avatar
3 Answers
+ 2
NINJA PEACE what if all the numbers are negative? Then your preset value of 0 will be the highest number and the returned value will be incorrect. Also, using 'max' as a variable name isn't a good idea as it will overwrite the built-in function. def maximum(no_list): m = no_list[0] for num in no_list[1:]: if num > m: m = num return m Or, if allowed; def maximum(no_list): return max(no_list) But this is really redundant, so probably not allowed.
26th Apr 2021, 7:02 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Negm Abozed I just supplied the maximum function. You have to supply the rest. If you can't figure it out or there are other issues, just saying it doesn't work, doesn't help. Post your code and what you have tried along with a good description of what the issue(s) is/are. More info is better than a vague useless reply.
26th Apr 2021, 6:07 PM
ChaoticDawg
ChaoticDawg - avatar
0
Negm Abozed Try this: ln [ ]: def maximum(no_list): max = 0 for x in no_list: if x > max: max = x print(max) maximum(no_list)
26th Apr 2021, 11:18 AM
NINJA PEACE
NINJA PEACE - avatar