-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.py
270 lines (156 loc) · 8.55 KB
/
sort.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# ECOR 1042 Lab 5 - Team submission
# Remember to include docstring and type annotations for your functions
# Update "" to list all students contributing to the team work
__author__ = "Kalid Alex Alham Caleb"
# Update "" with your team (e.g. T102)
__team__ = "T058"
#==========================================#
# Place your sort_students_age_bubble function after this line
def sort_students_age_bubble(students: list, order) -> list:
"""
This function sorts students list in ascending or descending order based on there Age.
If the Age key is not in the dictionary the function will print out a statement.
Preconditions: order = A or D
Examples:
>>> sort_students_age_bubble([{"Age":10,"School":"GP"},{"Age":19,"School":"MS"}], "D")
[{"Age": 19, "School":"MS"}, {"Age":10, "School":"GP"}]
>>> sort_students_age_bubble([{"Age":20,"School":"GP"},{"Age":19,"School":"MS"}], "A")
[{"Age": 19, "School":"MS"}, {"Age":20, "School":"GP"}]
>>> sort_students_age_bubble([{"School":"GP"}, {"School":"MS"}], "D")
"Age" key is not present.
[{"School":"GP"}, {"School":"MS"}]
"""
n = len(students)
while True:
swapped = False
for i in range(n-1):
if "Age" in students[i] and "Age" in students[i+1]:
if order == "A":
if students[i]["Age"] > students[i+1]["Age"]:
students[i], students[i+1] = students[i+1], students[i]
swapped = True
elif order == "D":
if students[i]["Age"] < students[i+1]["Age"]:
students[i], students[i + 1] = students[i + 1], students[i]
swapped = True
else:
print("Invalid order parameter. Please enter 'A' for ascending or 'D' for descending.")
return students
else:
print("Error: key 'Age' not found in the list of students")
return students
if not swapped:
break
return students
#==========================================#
# Place your sort_students_time_selection function after this line
def sort_students_time_selection(students_list: list, order: str) -> list:
for i in range(len(students_list)):
if "StudyTime" in students_list[i]:
for j in range(i + 1, len(students_list)):
if "StudyTime" in students_list[j]:
if order == "A":
if students_list[j]["StudyTime"] < students_list[i]["StudyTime"]:
students_list[i], students_list[j] = students_list[j], students_list[i]
elif order == "D":
if students_list[j]["StudyTime"] > students_list[i]["StudyTime"]:
students_list[i], students_list[j] = students_list[j], students_list[i]
else:
print("Invalid order parameter. Please enter 'A' for ascending or 'D' for descending.")
return students_list
else:
print("StudyTime key is not present.")
return students_list
return students_list
#==========================================#
# Place your sort_students_g_avg_insertion function after this line
def sort_students_g_avg_insertion(list_of_students: list, order_type:str) -> list:
"""
Sorts list using insertion sort
Precondition order = A or D
Example:
>>>sort_students_g_avg_insertion( [{"G_Avg":7.2,"School":"GP"}, {"G_Avg":9.1,"School":"MS"}], "D")
[{"G_Avg": 9.1, "School":"MS"}, {"G_Avg":7.2, "School":"GP"}]
"""
m = len(list_of_students)
for i in range(0, m):
if 'G_Avg' not in list_of_students[i]:
print('the key is not in the dictionary')
else:
if order_type == 'D':
if (n := len(list_of_students)) <= 1:
return
for i in range(1, n):
key = list_of_students[i]['G_Avg']
j = i-1
while j >=0 and key > (list_of_students[j]['G_Avg']):
list_of_students[j+1]['G_Avg'] = list_of_students[j]['G_Avg']
j -= 1
list_of_students[j+1]['G_Avg'] = key
if order_type == 'A':
if (n := len(list_of_students)) <= 1:
return
for i in range(1, n):
key = list_of_students[i]['G_Avg']
j = i-1
while j >=0 and key < (list_of_students[j]['G_Avg']):
list_of_students[j+1]['G_Avg'] = list_of_students[j]['G_Avg']
j -= 1
list_of_students[j+1]['G_Avg'] = key
return list_of_students
#==========================================#
# Place your sort_students_failures_bubble function after this line
def sort_students_failures_bubble(students: list, order) -> list:
"""
This function sorts students list in ascending or descending order based on there Age.
If the Age key is not in the dictionary the function will print out a statement.
Preconditions: order = A or D
Examples:
"""
n = len(students)
while True:
swapped = False
for i in range(n-1):
if "Failures" in students[i] and "Failures" in students[i + 1]:
if order == "A":
if students[i]["Failures"] > students[i + 1]["Failures"]:
students[i], students[i+1] = students[i+1], students[i]
swapped = True
elif order == "D":
if students[i]["Failures"] < students[i + 1]["Failures"]:
students[i], students[i + 1] = students[i + 1], students[i]
swapped = True
else:
print("Invalid order parameter. Please enter 'A' for ascending or 'D' for descending.")
return students
else:
print("Error: key 'Failures' not found in the list of students")
return students
if not swapped:
break
return students
#==========================================#
# Place your sort function after this line
def sort(student_list: list, order: str, sorting_type: str) -> list:
"""
Lets the user decide what to sort in the list of dictionaries
Precondition: order = A or D
Examples:
>>>sort([{"Age":10,"School":"GP"},{"Age":19.1,"School":"MS"}],"D","Age")
[{"Age": 19, "School":"MS"}, {"Age":10, "School":"GP"}]
>>>sort([{"School":"GP"},{"School":"MS"}], "D", "School")
Cannot be sorted by "School"
[{"School":"GP"}, {"School":"MS"}]
"""
if sorting_type == "Age":
sort_students_age_bubble(student_list, order)
elif sorting_type == "Failures":
sort_students_failures_bubble(student_list, order)
elif sorting_type == "G_avg":
sort_students_g_avg_insertion(student_list, order)
elif sorting_type == "StudyTime":
sort_students_time_selection(student_list, order)
else:
print('Cannot be sorted by "'+ sorting_type + '"')
return student_list
# Do NOT include a main script in your submission