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.
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]
Comments
Leave a comment