Answer to Question #303401 in Python for divya

Question #303401

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.


1
Expert's answer
2022-02-28T01:06:11-0500
def secondLarge(arr: list):
    for element in arr:
        if not isinstance(element,int): return -999
    if len(arr) == 1:
        return arr[-1]
    else:
        arr.sort()
        return arr[-2]

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