-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnested-list.py
130 lines (111 loc) · 4.59 KB
/
nested-list.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
https://www.hackerrank.com/challenges/nested-list/problem
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.
Note: If there are multiple students with the second lowest grade,
order their names alphabetically and print each name on a new line.
"""
# The first line contains an integer, "student_number" the number of students.
student_number = int(input())
temp_list = [] # temp_list to save all the second lowest scores.
student_list = [] # student_list to save all student names and grades.
def get_student_info():
"""
This function will get the student name and grade from the user and save it in sub list
"""
# The subsequent lines describe each student over lines.
# - The first line contains a student's name.
# - The second line contains their grade.
student_name = str(input())
student_grade = float(input())
student_sub_list = [student_name, student_grade]
return student_sub_list
def adding_student_info(student_sub_list):
"""
This function will add sub list of student informatio to the main list
"""
student_list.append(student_sub_list)
return student_list
def sorting_students_list(student_list):
"""
This function will sort the students acording to their grades in the main list.
"""
length = len(student_list)
for i in range(0, length):
for j in range(0, length - i - 1):
if student_list[j][1] >= 0:
if student_list[j][1] > student_list[j+1][1]:
temp = student_list[j]
student_list[j] = student_list[j+1]
student_list[j+1] = temp
else:
temp = student_list[j]
student_list[j] = student_list[j+1]
student_list[j+1] = temp
return student_list
def check_second_lowest_number(student_list):
"""
This function will search for all second lowest scores, and
sort them alphapetical and then print them.
"""
length = len(student_list)
for i in range(0, length-1):
for j in range(0, length-1):
if student_list[j + 1][1] == student_list[i][1]:
pass
elif student_list[j+1][1] > student_list[j][1]:
for x in range(0, length - 1):
if student_list[x+1][1] == student_list[j + 1][1]:
temp_list.append(student_list[x+1])
temp = sorting_lowest_score_list(temp_list)
printing_lowest_score_list(temp)
break
elif student_list[j + 1][1] == student_list[j][1]:
temp_list.append(student_list[j])
for x in range(0, length - 1):
if student_list[x+1][1] == student_list[j + 1][1]:
temp_list.append(student_list[x+1])
temp = sorting_lowest_score_list(temp_list)
printing_lowest_score_list(temp)
break
elif student_list[j][1] < 0:
for x in range(0, length - 1):
if student_list[x+1][1] == student_list[j + 1][1]:
temp_list.append(student_list[x+1])
temp = sorting_lowest_score_list(temp_list)
printing_lowest_score_list(temp)
break
else:
temp_list.append(student_list[j])
temp = sorting_lowest_score_list(temp_list)
printing_lowest_score_list(temp)
break
break
return temp_list
def sorting_lowest_score_list(temp_list):
length = len(temp_list)
for i in range(0, length):
for j in range(0, length - 1):
if temp_list[j][0] > temp_list[j+1][0]:
temp = temp_list[j]
temp_list[j] = temp_list[j+1]
temp_list[j+1] = temp
return temp_list
def printing_lowest_score_list(temp_list):
length = len(temp_list)
for i in range(0, length):
for j in range(0, length):
print(temp_list[j][0])
break
################ Main While Loop ######################
while student_number > 0:
student_sub_list = get_student_info()
student_list = adding_student_info(student_sub_list)
for student in student_list:
student_number -= 1
if student_number == 0:
break
student_sub_list = get_student_info()
student_list = adding_student_info(student_sub_list)
student_list = sorting_students_list(student_list)
temp_list = check_second_lowest_number(student_list)