hnc-vitalii/HNC/Arrays/Find_Second _Max.py

42 lines
1.2 KiB
Python

def find_second_max(array):
if array and len(array) > 0:
array.sort()
my_max = array[0]
my_second_max = None
for i in array:
if i > my_max:
my_second_max = my_max
my_max = i
elif i < my_max:
if my_second_max is None:
my_second_max = i
else:
if i > my_max:
my_second_max = i
return my_second_max
return None
#################################################
def verifier(test_name, actual, expected):
print(test_name)
if actual != expected:
print("=======> FAILED! <=======")
print(f"Got value <{actual}>")
print(f"Expected value is <{expected}>")
else:
print("PASSED")
print()
second_max_tests = [([1, 6, 3, 6, 54, 3, 6, 3, 1, 2, 8], 8),
([2, 2, 2, 2, 2, 2, 2, 2, 2], None),
([2, 2, 2, 2, 2, 2, 2, 2, 4], 2),
([], None),
([1, 0, -1], 0),
([4, -7, 6, 23, -5, 23, -4, 5, 7, 100], 23)]
for i in range(len(second_max_tests)):
verifier(f"Second max test {i+1}", find_second_max(second_max_tests[i][0]), second_max_tests[i][1])