Spaces:
No application file
No application file
File size: 4,426 Bytes
b7926b9 |
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 |
'''
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.
'''
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)))
#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:]))
#
## 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)
|