-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_scores.py
69 lines (54 loc) · 1.53 KB
/
sort_scores.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'''
HackerRank training
Given the names and grades for each student in a class of students,
store them in a nested list and print the name(s) of any student(s) having the second lowest grade.
Implemented in two ways.
Sample input:
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
Sample output:
Berry
Harry
'''
#from timeit import timeit
def processWithLists():
names, scores, students = [], [], []
for _ in range(int(input())): # Number of students
name = input()
score = float(input())
scores.append(score)
students.append([name, score])
print(students)
# Ordered list of unique scores
scores = sorted(set(scores))
secLowest = scores[1] if len(scores) > 1 else None # 2nd lowest
names = [s[0] for s in students if s[1] == secLowest]
return names
def processWithDict():
students = []
for _ in range(int(input())):
students.append( {'name': input(), 'score': float(input())} )
# Sort by score & name
students.sort(key=lambda s: (s['score'], s['name']))
print(students)
#breakpoint()
scores = list(set([s['score'] for s in students]))
secLowest = scores[1] if len(scores) > 1 else None # 2nd lowest
names = [ s['name'] for s in students if s['score'] == secLowest ]
return names
if __name__ == '__main__':
print("Awaiting input data...")
names = processWithDict()
#names = processWithLists()
print(f"\n{'-'*10} Students with the second lowest score are:")
for n in sorted(names):
print(n)