Spaces:
No application file
No application file
Sort INPLACE. list.sort(key=lambda list: max(list[1:]))
Browse files
Week 7 Libraries and More Python/{15. Sort by best exam score → [FIXED]15. Sort by best exam score}
RENAMED
|
@@ -12,6 +12,29 @@ Note! You can either use a lambda statement or write your own value function. Th
|
|
| 12 |
|
| 13 |
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def sortby_result(students: list) -> list:
|
| 16 |
"""
|
| 17 |
Sort students based on their best exam score in ascending order.
|
|
@@ -38,24 +61,30 @@ def sortby_result(students: list) -> list:
|
|
| 38 |
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 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
|
|
@@ -161,3 +190,37 @@ print(sorted_students)
|
|
| 161 |
|
| 162 |
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# approach 1
|
| 18 |
+
#def sortby_result(students: list) -> list:
|
| 19 |
+
# """
|
| 20 |
+
# Sort students based on their best exam score in descending order.
|
| 21 |
+
#
|
| 22 |
+
# Parameters:
|
| 23 |
+
# students: List of tuples, where each tuple contains:
|
| 24 |
+
# (student_name: str, exam1: int, exam2: int, exam3: int)
|
| 25 |
+
#
|
| 26 |
+
# Returns:
|
| 27 |
+
# List of student tuples sorted by their highest exam score in ascending order
|
| 28 |
+
# """
|
| 29 |
+
# # Using lambda function to sort
|
| 30 |
+
# # max(student[1:]) gets the maximum value from the three exam scores
|
| 31 |
+
# return sorted(students, key=lambda student: max(student[1:]))
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# approach 2-4
|
| 38 |
def sortby_result(students: list) -> list:
|
| 39 |
"""
|
| 40 |
Sort students based on their best exam score in ascending order.
|
|
|
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
+
# approach 5
|
| 65 |
+
def sortby_result(students: list) -> list:
|
| 66 |
+
"""
|
| 67 |
+
Sort students based on their best exam score in ascending order.
|
| 68 |
+
|
| 69 |
+
Parameters:
|
| 70 |
+
students: List of tuples, where each tuple contains:
|
| 71 |
+
(student_name: str, exam1: int, exam2: int, exam3: int)
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
List of student tuples sorted by their highest exam score in ascending order.
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
students.sort(key=lambda student: max(student[1:]))
|
| 78 |
+
# Return the sorted list (optional, since the list is modified in-place)
|
| 79 |
+
return students
|
| 80 |
+
|
| 81 |
+
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
+
#########################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
# approach 1-4
|
| 88 |
|
| 89 |
#
|
| 90 |
## Test cases
|
|
|
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
#########################
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
# approach 5
|
| 201 |
+
|
| 202 |
+
Original order:
|
| 203 |
+
('Jorge', 80, 46, 36)
|
| 204 |
+
('Dave', 65, 28, 100)
|
| 205 |
+
('Stephanie', 14, 38, 36)
|
| 206 |
+
('Wade', 69, 40, 68)
|
| 207 |
+
('Arthur', 26, 15, 41)
|
| 208 |
+
('Brian', 21, 93, 88)
|
| 209 |
+
('Dan', 5, 6, 60)
|
| 210 |
+
('Vera', 70, 81, 99)
|
| 211 |
+
|
| 212 |
+
Sorted by best exam result:
|
| 213 |
+
('Stephanie', 14, 38, 36)
|
| 214 |
+
('Arthur', 26, 15, 41)
|
| 215 |
+
('Dan', 5, 6, 60)
|
| 216 |
+
('Wade', 69, 40, 68)
|
| 217 |
+
('Jorge', 80, 46, 36)
|
| 218 |
+
('Brian', 21, 93, 88)
|
| 219 |
+
('Vera', 70, 81, 99)
|
| 220 |
+
('Dave', 65, 28, 100)
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
|