-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW3_PA2.py
45 lines (31 loc) · 1.03 KB
/
W3_PA2.py
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
34
35
36
37
38
39
40
41
42
43
44
45
# Given a list of numbers (integers), find second maximum and second minimum in this list.
# Input Format:
# The first line contains numbers separated by a space.
# Output Format:
# Print second maximum and second minimum separated by a space
# Example:
# Input:
# 1 2 3 4 5
# Output:
# 4 2
# -----------------------------------------------------------------------------------------------------------------
NumList = list(map(int, input().strip().split()))[:]
first = second = NumList[0]
for j in range(len(NumList)):
# For second maximum:
if NumList[j] > first:
second = first
first = NumList[j]
else:
if (NumList[j] > second) and (NumList[j] < first):
second = NumList[j]
print(second, end=" ")
for j in range(len(NumList)):
# For second minimum:
if NumList[j] < first:
second = first
first = NumList[j]
else:
if (NumList[j] < second) and (NumList[j] != first):
second = NumList[j]
print(second, end="")