-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsecond_min_max.py
More file actions
33 lines (28 loc) · 1.2 KB
/
Copy pathsecond_min_max.py
File metadata and controls
33 lines (28 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
list1 = [1, 2, 3, 66, 56, 45, 4, 404, 45]
# Initialize min, max, second_min, second_max
min_value = float('inf') # Positive infinity (for min)
max_value = float('-inf') # Negative infinity (for max)
second_min = float('inf') # Positive infinity (for second min)
second_max = float('-inf') # Negative infinity (for second max)
for num in list1:
# Update min and second_min
if num < min_value:
second_min = min_value # Update second_min before min
min_value = num
elif num < second_min and num != min_value: # Update second_min if it's not equal to min_value
second_min = num
# Update max and second_max
if num > max_value:
second_max = max_value # Update second_max before max
max_value = num
elif num > second_max and num != max_value: # Update second_max if it's not equal to max_value
second_max = num
# Check if second min and second max exist
if second_min == float('inf'):
print("There is no second minimum value")
else:
print("The second minimum value is:", second_min)
if second_max == float('-inf'):
print("There is no second maximum value")
else:
print("The second maximum value is:", second_max)