Spaces:
No application file
No application file
SORT INPLACE. list.sort(key=some_inner_fn)
Browse files
Week 7 Libraries and More Python/16. Sort by the difference of goals
CHANGED
|
@@ -16,8 +16,8 @@ Note! You can either use a lambda statement or write your own value function. Th
|
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
def sortby_goalsdiff(teams: list):
|
| 22 |
def get_goal_difference(team):
|
| 23 |
goals_for, goals_against = map(int, team[1].split(' - '))
|
|
@@ -148,3 +148,75 @@ print(sortby_goalsdiff(teams))
|
|
| 148 |
('Pelicans', '120 - 45')
|
| 149 |
('Sport', '26 - 165')
|
| 150 |
('Lukko', '119 - 192')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
+
#########################
|
| 20 |
+
# approach 1
|
| 21 |
def sortby_goalsdiff(teams: list):
|
| 22 |
def get_goal_difference(team):
|
| 23 |
goals_for, goals_against = map(int, team[1].split(' - '))
|
|
|
|
| 148 |
('Pelicans', '120 - 45')
|
| 149 |
('Sport', '26 - 165')
|
| 150 |
('Lukko', '119 - 192')
|
| 151 |
+
|
| 152 |
+
############################
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
# approach 2
|
| 156 |
+
|
| 157 |
+
def sortby_goalsdiff(teams: list) -> list:
|
| 158 |
+
"""
|
| 159 |
+
Sort teams based on their goal difference in ascending order.
|
| 160 |
+
|
| 161 |
+
Parameters:
|
| 162 |
+
teams: List of tuples, where each tuple contains:
|
| 163 |
+
(team_name: str, goals_for - goals_against: str)
|
| 164 |
+
|
| 165 |
+
Returns:
|
| 166 |
+
List of team tuples sorted by their goal difference in ascending order.
|
| 167 |
+
"""
|
| 168 |
+
|
| 169 |
+
# Define a helper function to calculate goal difference
|
| 170 |
+
def get_goal_difference(team):
|
| 171 |
+
goals_for, goals_against = map(int, team[1].split(' - '))
|
| 172 |
+
return goals_for - goals_against
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
# Sort the teams list INPLACE using the goal difference as the key
|
| 176 |
+
teams.sort(key=get_goal_difference)
|
| 177 |
+
# Return the sorted list
|
| 178 |
+
return teams
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
Original order:
|
| 186 |
+
('Kalpa', '123 - 61')
|
| 187 |
+
('Pelicans', '99 - 151')
|
| 188 |
+
('Tappara', '29 - 46')
|
| 189 |
+
('Ilves', '179 - 56')
|
| 190 |
+
('Jukurit', '32 - 60')
|
| 191 |
+
('JYP', '108 - 112')
|
| 192 |
+
('Lukko', '147 - 115')
|
| 193 |
+
('Ässät', '37 - 35')
|
| 194 |
+
('Kärpät', '179 - 165')
|
| 195 |
+
('Saipa', '78 - 102')
|
| 196 |
+
('TPS', '197 - 164')
|
| 197 |
+
('KooKoo', '89 - 132')
|
| 198 |
+
('HIFK', '48 - 136')
|
| 199 |
+
('HPK', '164 - 175')
|
| 200 |
+
('Sport', '168 - 153')
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
Sorted by the goal difference:
|
| 204 |
+
('HIFK', '48 - 136')
|
| 205 |
+
('Pelicans', '99 - 151')
|
| 206 |
+
('KooKoo', '89 - 132')
|
| 207 |
+
('Jukurit', '32 - 60')
|
| 208 |
+
('Saipa', '78 - 102')
|
| 209 |
+
('Tappara', '29 - 46')
|
| 210 |
+
('HPK', '164 - 175')
|
| 211 |
+
('JYP', '108 - 112')
|
| 212 |
+
('Ässät', '37 - 35')
|
| 213 |
+
('Kärpät', '179 - 165')
|
| 214 |
+
('Sport', '168 - 153')
|
| 215 |
+
('Lukko', '147 - 115')
|
| 216 |
+
('TPS', '197 - 164')
|
| 217 |
+
('Kalpa', '123 - 61')
|
| 218 |
+
('Ilves', '179 - 56')
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
|