Answer to Question #303095 in Python for rgtd

Question #303095

Define a function named "secondLarge" which take a list of numbers (integer) as parameter.


This function will then identify and return the 2nd largest number in the list. The function will


return smallest number if the parameter only contains two numbers. While the function will


return the only number if the list contain only single number. On some odd case, the function


will return -999 if the parameter contains other datatype i.e. string or floaat in the list.


FOR EXAMPLE:


• [1,2,3,4,5,6] returns 5


• [6,8,3,4,6] returns 6


• [53, 23] return 23


• [13] return 13


• [12, 'not number', 23] return -99220


1
Expert's answer
2022-02-27T07:02:52-0500
def secondLarge(list):
    for i in list:
        if type(i) != int:
            return -999
    if len(list) >= 2:
        list.remove(max(list))
        return(max(list))
    else:
        return list[0]

print(secondLarge([1, 5, 7, 10, 2, 4]))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS