File size: 5,509 Bytes
b7926b9
 
 
 
 
 
 
 
 
 
 
 
 
 
a83ed65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7926b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a83ed65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7926b9
 
 
a83ed65
b7926b9
a83ed65
b7926b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a83ed65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
Write a function

sortby_result(students: list)

which takes a list of tuples as its parameter. A single tuple represents the student's exam results. The first tuple is the student's name (string) and the next three are the student's scores (integers) in the three different exams of the course. Since only the best exam result for each student is taken into account, the exam list should be arranged in ascending order so that only the best of the three exam results for each student is taken into account as a ranking criterion.

Note! You can either use a lambda statement or write your own value function. This does not affect the marking.
'''







# approach 1
#def sortby_result(students: list) -> list:
#    """
#    Sort students based on their best exam score in descending order.
#    
#    Parameters:
#        students: List of tuples, where each tuple contains:
#                 (student_name: str, exam1: int, exam2: int, exam3: int)
#    
#    Returns:
#        List of student tuples sorted by their highest exam score in ascending order
#    """
#    # Using lambda function to sort
#    # max(student[1:]) gets the maximum value from the three exam scores
#    return sorted(students, key=lambda student: max(student[1:]))





# approach 2-4
def sortby_result(students: list) -> list:
    """
    Sort students based on their best exam score in ascending order.
    
    Parameters:
        students: List of tuples, where each tuple contains:
                 (student_name: str, exam1: int, exam2: int, exam3: int)
    
    Returns:
        List of student tuples sorted by their highest exam score in ascending order.
    """
    list_student_max_score = [(student[0], max(student[1:])) for student in students]
    sorted_student_max_score = sorted(list_student_max_score, key=lambda x: x[1])
    sorted_student_names = [student[0] for student in sorted_student_max_score]
    
    student_dict = {student[0]: student for student in students}
    sorted_students = [student_dict[name] for name in sorted_student_names]
    
    return sorted_students
    
#    return sorted(students, key=lambda student: max(student[1:]))
#    return sorted(students, key=lambda student: (max(student[1:]), students.index(student)))




# approach 5
def sortby_result(students: list) -> list:
    """
    Sort students based on their best exam score in ascending order.
    
    Parameters:
        students: List of tuples, where each tuple contains:
                 (student_name: str, exam1: int, exam2: int, exam3: int)
    
    Returns:
        List of student tuples sorted by their highest exam score in ascending order.
    """
    
    students.sort(key=lambda student: max(student[1:]))
    # Return the sorted list (optional, since the list is modified in-place)
    return students





#########################

# approach 1-4

#
## Test cases
#test_students = [
#    ("Alice", 85, 92, 78),
#    ("Bob", 76, 88, 95),
#    ("Charlie", 89, 72, 84)
#]
#
## Running the test
#sorted_students = sortby_result(test_students)
#
## Demonstrating results
#print("Original students list:")
#for student in test_students:
#    print(f"Name: {student[0]}, Scores: {student[1:]}, Best Score: {max(student[1:])}")
#
#print("\nSorted students list:")
#for student in sorted_students:
#    print(f"Name: {student[0]}, Scores: {student[1:]}, Best Score: {max(student[1:])}")







# Example1 from my python IDE:
students = [
    ('Sabrina', 52, 39, 6),
    ('Brian', 26, 73, 53),
    ('Dave', 99, 92, 20),
    ('Stephanie', 71, 81, 13),
    ('Janet', 56, 75, 100),
    ('Arthur', 13, 63, 18),
    ('Ivan', 54, 36, 12),
    ('Roberto', 37, 100, 19),
    ('Jorge', 88, 32, 70),
    ('Seth', 32, 19, 8)
]

sorted_students = sortby_result(students)
print(sorted_students)
[('Seth', 32, 19, 8), ('Sabrina', 52, 39, 6), ('Ivan', 54, 36, 12), ('Arthur', 13, 63, 18), ('Brian', 26, 73, 53), ('Stephanie', 71, 81, 13), ('Jorge', 88, 32, 70), ('Dave', 99, 92, 20), ('Janet', 56, 75, 100), ('Roberto', 37, 100, 19)]










# Example2
#Original order:
students = [
('Ivan', 56, 68, 17)
,('Seth', 35, 76, 39)
,('Lauren', 2, 76, 63)
,('Isabel', 32, 48, 66)
,('Lucy', 48, 14, 22)
,('Janet', 42, 33, 96)
,('Brian', 42, 57, 43)
,('Vicki', 19, 54, 20)
]


# autograder expected results
Sorted by best exam result:
('Lucy', 48, 14, 22)
('Vicki', 19, 54, 20)
('Brian', 42, 57, 43)
('Isabel', 32, 48, 66)
('Ivan', 56, 68, 17)
('Seth', 35, 76, 39)
('Lauren', 2, 76, 63)
('Janet', 42, 33, 96)




# Example 2 my Python IDE results - matches expected output
sorted_students = sortby_result(students)
print(sorted_students)
[('Lucy', 48, 14, 22), ('Vicki', 19, 54, 20), ('Brian', 42, 57, 43), ('Isabel', 32, 48, 66), ('Ivan', 56, 68, 17), ('Seth', 35, 76, 39), ('Lauren', 2, 76, 63), ('Janet', 42, 33, 96)]







# autograder actual results - not sure why different from my Python IDE results
('Ivan', 56, 68, 17)
('Seth', 35, 76, 39)
('Lauren', 2, 76, 63)
('Isabel', 32, 48, 66)
('Lucy', 48, 14, 22)
('Janet', 42, 33, 96)
('Brian', 42, 57, 43)
('Vicki', 19, 54, 20)






#########################



# approach 5

Original order:
('Jorge', 80, 46, 36)
('Dave', 65, 28, 100)
('Stephanie', 14, 38, 36)
('Wade', 69, 40, 68)
('Arthur', 26, 15, 41)
('Brian', 21, 93, 88)
('Dan', 5, 6, 60)
('Vera', 70, 81, 99)

Sorted by best exam result:
('Stephanie', 14, 38, 36)
('Arthur', 26, 15, 41)
('Dan', 5, 6, 60)
('Wade', 69, 40, 68)
('Jorge', 80, 46, 36)
('Brian', 21, 93, 88)
('Vera', 70, 81, 99)
('Dave', 65, 28, 100)