-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHighNLow.py
More file actions
80 lines (67 loc) · 2.52 KB
/
HighNLow.py
File metadata and controls
80 lines (67 loc) · 2.52 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#Sorting without built in functions
n = 10
lowList = []
highList = []
def numHigh(numList):
n = 10
for i in range(n):
for j in range(i+1, n):
if(numList[i] < numList[j]):
temp = numList[i]
numList[i] = numList[j]
numList[j] = temp
print("first to the highest: ", numList[0])
print("second to the highest: ", numList[1])
def numLow(numList):
n = 10
for i in range(n):
for j in range(i+1, n):
if(numList[i] > numList[j]):
temp = numList[i]
numList[i] = numList[j]
numList[j] = temp
print("first to the lowest: ", numList[0])
print("second to the lowest: ", numList[1])
def main():
numList = list(map(int,input("Enter 10 numbers : \n").strip().split()))[:n]
numHigh(numList)
numLow(numList)
def highNlow():
#numbers input split by space
num = input("Enter 10 numbers:\n").split(' ')
try:
#convert into integer, stored in an array
arr = [int(num) for num in num]
#use to count number of elements in an array
elements = len(arr)
if elements == 10:
#Sort the array in ascending order
temp = 0;
for i in range(0, len(arr)):
for j in range(i+1, len(arr)):
if(arr[i] > arr[j]):
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
#dictionaries can't have duplicated keys
duplicated = list(dict.fromkeys(arr))
print (duplicated)
#used to count number of elements in filtered list
repeated = len(duplicated)
if repeated == 1:
print("\nfirst to the highest: ", duplicated[0]);
print("second to the highest: ", duplicated[0]);
print("second to the lowest: ", duplicated[0]);
print("first to the lowest: ", duplicated[0]);
else:
#use negative indices to get the end of an array
print("\nfirst to the highest: ", duplicated[-1]);
print("second to the highest: ", duplicated[-2])
print("second to the lowest: ", duplicated[1])
print("first to the lowest: ", duplicated[0])
else:
print("Please input 10 numbers!")
except:
print("Invalid input! Please enter type:integers only!")
main()
#highNlow()