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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''
|
| 2 |
+
Write a function
|
| 3 |
+
|
| 4 |
+
sortby_result(students: list)
|
| 5 |
+
|
| 6 |
+
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.
|
| 7 |
+
|
| 8 |
+
Note! You can either use a lambda statement or write your own value function. This does not affect the marking.
|
| 9 |
+
'''
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def sortby_result(students: list) -> list:
|
| 16 |
+
"""
|
| 17 |
+
Sort students based on their best exam score in ascending order.
|
| 18 |
+
|
| 19 |
+
Parameters:
|
| 20 |
+
students: List of tuples, where each tuple contains:
|
| 21 |
+
(student_name: str, exam1: int, exam2: int, exam3: int)
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
List of student tuples sorted by their highest exam score in ascending order.
|
| 25 |
+
"""
|
| 26 |
+
list_student_max_score = [(student[0], max(student[1:])) for student in students]
|
| 27 |
+
sorted_student_max_score = sorted(list_student_max_score, key=lambda x: x[1])
|
| 28 |
+
sorted_student_names = [student[0] for student in sorted_student_max_score]
|
| 29 |
+
|
| 30 |
+
student_dict = {student[0]: student for student in students}
|
| 31 |
+
sorted_students = [student_dict[name] for name in sorted_student_names]
|
| 32 |
+
|
| 33 |
+
return sorted_students
|
| 34 |
+
|
| 35 |
+
# return sorted(students, key=lambda student: max(student[1:]))
|
| 36 |
+
# return sorted(students, key=lambda student: (max(student[1:]), students.index(student)))
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
#def sortby_result(students: list) -> list:
|
| 45 |
+
# """
|
| 46 |
+
# Sort students based on their best exam score in descending order.
|
| 47 |
+
#
|
| 48 |
+
# Parameters:
|
| 49 |
+
# students: List of tuples, where each tuple contains:
|
| 50 |
+
# (student_name: str, exam1: int, exam2: int, exam3: int)
|
| 51 |
+
#
|
| 52 |
+
# Returns:
|
| 53 |
+
# List of student tuples sorted by their highest exam score in ascending order
|
| 54 |
+
# """
|
| 55 |
+
# # Using lambda function to sort
|
| 56 |
+
# # max(student[1:]) gets the maximum value from the three exam scores
|
| 57 |
+
# return sorted(students, key=lambda student: max(student[1:]))
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
#
|
| 61 |
+
## Test cases
|
| 62 |
+
#test_students = [
|
| 63 |
+
# ("Alice", 85, 92, 78),
|
| 64 |
+
# ("Bob", 76, 88, 95),
|
| 65 |
+
# ("Charlie", 89, 72, 84)
|
| 66 |
+
#]
|
| 67 |
+
#
|
| 68 |
+
## Running the test
|
| 69 |
+
#sorted_students = sortby_result(test_students)
|
| 70 |
+
#
|
| 71 |
+
## Demonstrating results
|
| 72 |
+
#print("Original students list:")
|
| 73 |
+
#for student in test_students:
|
| 74 |
+
# print(f"Name: {student[0]}, Scores: {student[1:]}, Best Score: {max(student[1:])}")
|
| 75 |
+
#
|
| 76 |
+
#print("\nSorted students list:")
|
| 77 |
+
#for student in sorted_students:
|
| 78 |
+
# print(f"Name: {student[0]}, Scores: {student[1:]}, Best Score: {max(student[1:])}")
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# Example1 from my python IDE:
|
| 87 |
+
students = [
|
| 88 |
+
('Sabrina', 52, 39, 6),
|
| 89 |
+
('Brian', 26, 73, 53),
|
| 90 |
+
('Dave', 99, 92, 20),
|
| 91 |
+
('Stephanie', 71, 81, 13),
|
| 92 |
+
('Janet', 56, 75, 100),
|
| 93 |
+
('Arthur', 13, 63, 18),
|
| 94 |
+
('Ivan', 54, 36, 12),
|
| 95 |
+
('Roberto', 37, 100, 19),
|
| 96 |
+
('Jorge', 88, 32, 70),
|
| 97 |
+
('Seth', 32, 19, 8)
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
sorted_students = sortby_result(students)
|
| 101 |
+
print(sorted_students)
|
| 102 |
+
[('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)]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# Example2
|
| 114 |
+
#Original order:
|
| 115 |
+
students = [
|
| 116 |
+
('Ivan', 56, 68, 17)
|
| 117 |
+
,('Seth', 35, 76, 39)
|
| 118 |
+
,('Lauren', 2, 76, 63)
|
| 119 |
+
,('Isabel', 32, 48, 66)
|
| 120 |
+
,('Lucy', 48, 14, 22)
|
| 121 |
+
,('Janet', 42, 33, 96)
|
| 122 |
+
,('Brian', 42, 57, 43)
|
| 123 |
+
,('Vicki', 19, 54, 20)
|
| 124 |
+
]
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
# autograder expected results
|
| 128 |
+
Sorted by best exam result:
|
| 129 |
+
('Lucy', 48, 14, 22)
|
| 130 |
+
('Vicki', 19, 54, 20)
|
| 131 |
+
('Brian', 42, 57, 43)
|
| 132 |
+
('Isabel', 32, 48, 66)
|
| 133 |
+
('Ivan', 56, 68, 17)
|
| 134 |
+
('Seth', 35, 76, 39)
|
| 135 |
+
('Lauren', 2, 76, 63)
|
| 136 |
+
('Janet', 42, 33, 96)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
# Example 2 my Python IDE results - matches expected output
|
| 142 |
+
sorted_students = sortby_result(students)
|
| 143 |
+
print(sorted_students)
|
| 144 |
+
[('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)]
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
# autograder actual results - not sure why different from my Python IDE results
|
| 153 |
+
('Ivan', 56, 68, 17)
|
| 154 |
+
('Seth', 35, 76, 39)
|
| 155 |
+
('Lauren', 2, 76, 63)
|
| 156 |
+
('Isabel', 32, 48, 66)
|
| 157 |
+
('Lucy', 48, 14, 22)
|
| 158 |
+
('Janet', 42, 33, 96)
|
| 159 |
+
('Brian', 42, 57, 43)
|
| 160 |
+
('Vicki', 19, 54, 20)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|