Spaces:
No application file
No application file
Create Week 7 Libraries and More Python/16. Sort by the difference of goals
Browse files
Week 7 Libraries and More Python/16. Sort by the difference of goals
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''
|
| 2 |
+
Write a function
|
| 3 |
+
|
| 4 |
+
sortby_goalsdiff(teams: list)
|
| 5 |
+
|
| 6 |
+
which takes as parameter a list of tuples. A single tuple represents the team's result for the current season. The first element of the tuple is the team name (string). The second element is also a string: it represents the goals scored and conceded by the team during the season. So an example of a tuple could be
|
| 7 |
+
|
| 8 |
+
("TPS", "201 - 51")
|
| 9 |
+
|
| 10 |
+
The goal difference is calculated by subtracting the goals conceded by the team from the goals scored. In this example, the goal difference would be 150 (i.e. 201 - 51).
|
| 11 |
+
|
| 12 |
+
Your task is to arrange the list in ascending order according to the goal difference between the teams: the team with the worst (i.e. lowest) goal difference comes first in the list and the best team comes last.
|
| 13 |
+
|
| 14 |
+
Note! You can either use a lambda statement or write your own value function. This does not affect the scoring.
|
| 15 |
+
'''
|
| 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(' - '))
|
| 24 |
+
return goals_for - goals_against
|
| 25 |
+
|
| 26 |
+
return sorted(teams, key=get_goal_difference)
|
| 27 |
+
|
| 28 |
+
# Example1:
|
| 29 |
+
teams = [
|
| 30 |
+
('Kalpa', '53 - 58')
|
| 31 |
+
,('Saipa', '144 - 21')
|
| 32 |
+
,('Tappara', '37 - 17')
|
| 33 |
+
,('Ilves', '16 - 121')
|
| 34 |
+
,('HIFK', '124 - 121')
|
| 35 |
+
,('Pelicans', '86 - 49')
|
| 36 |
+
,('Sport', '180 - 48')
|
| 37 |
+
,('KooKoo', '35 - 87')
|
| 38 |
+
,('HPK', '130 - 52')
|
| 39 |
+
,('Kärpät', '58 - 15')
|
| 40 |
+
,('Jukurit', '34 - 133')
|
| 41 |
+
,('Lukko', '195 - 10')
|
| 42 |
+
,('JYP', '107 - 45')
|
| 43 |
+
,('Ässät', '98 - 170')
|
| 44 |
+
,('TPS', '197 - 109')
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
print(sortby_goalsdiff(teams))
|
| 49 |
+
[('Ilves', '16 - 121'), ('Jukurit', '34 - 133'), ('Ässät', '98 - 170'), ('KooKoo', '35 - 87'), ('Kalpa', '53 - 58'), ('HIFK', '124 - 121'), ('Tappara', '37 - 17'), ('Pelicans', '86 - 49'), ('Kärpät', '58 - 15'), ('JYP', '107 - 45'), ('HPK', '130 - 52'), ('TPS', '197 - 109'), ('Saipa', '144 - 21'), ('Sport', '180 - 48'), ('Lukko', '195 - 10')]
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Example2
|
| 65 |
+
# Autograder input
|
| 66 |
+
# Original order:
|
| 67 |
+
('Ilves', '26 - 110')
|
| 68 |
+
('HPK', '32 - 74')
|
| 69 |
+
('KooKoo', '137 - 27')
|
| 70 |
+
('Ässät', '64 - 77')
|
| 71 |
+
('Tappara', '23 - 28')
|
| 72 |
+
('HIFK', '135 - 27')
|
| 73 |
+
('Saipa', '157 - 15')
|
| 74 |
+
('JYP', '28 - 75')
|
| 75 |
+
('Jukurit', '136 - 159')
|
| 76 |
+
('Kalpa', '11 - 53')
|
| 77 |
+
('Kärpät', '35 - 193')
|
| 78 |
+
('TPS', '86 - 137')
|
| 79 |
+
('Pelicans', '120 - 45')
|
| 80 |
+
('Sport', '26 - 165')
|
| 81 |
+
('Lukko', '119 - 192')
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# Expected output
|
| 89 |
+
# Sorted by the goal difference:
|
| 90 |
+
('Kärpät', '35 - 193')
|
| 91 |
+
('Sport', '26 - 165')
|
| 92 |
+
('Ilves', '26 - 110')
|
| 93 |
+
('Lukko', '119 - 192')
|
| 94 |
+
('TPS', '86 - 137')
|
| 95 |
+
('JYP', '28 - 75')
|
| 96 |
+
('HPK', '32 - 74')
|
| 97 |
+
('Kalpa', '11 - 53')
|
| 98 |
+
('Jukurit', '136 - 159')
|
| 99 |
+
('Ässät', '64 - 77')
|
| 100 |
+
('Tappara', '23 - 28')
|
| 101 |
+
('Pelicans', '120 - 45')
|
| 102 |
+
('HIFK', '135 - 27')
|
| 103 |
+
('KooKoo', '137 - 27')
|
| 104 |
+
('Saipa', '157 - 15')
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
# Actual output by my python Terminal
|
| 110 |
+
teams = [
|
| 111 |
+
('Ilves', '26 - 110')
|
| 112 |
+
,('HPK', '32 - 74')
|
| 113 |
+
,('KooKoo', '137 - 27')
|
| 114 |
+
,('Ässät', '64 - 77')
|
| 115 |
+
,('Tappara', '23 - 28')
|
| 116 |
+
,('HIFK', '135 - 27')
|
| 117 |
+
,('Saipa', '157 - 15')
|
| 118 |
+
,('JYP', '28 - 75')
|
| 119 |
+
,('Jukurit', '136 - 159')
|
| 120 |
+
,('Kalpa', '11 - 53')
|
| 121 |
+
,('Kärpät', '35 - 193')
|
| 122 |
+
,('TPS', '86 - 137')
|
| 123 |
+
,('Pelicans', '120 - 45')
|
| 124 |
+
,('Sport', '26 - 165')
|
| 125 |
+
,('Lukko', '119 - 192')
|
| 126 |
+
]
|
| 127 |
+
print(sortby_goalsdiff(teams))
|
| 128 |
+
[('Kärpät', '35 - 193'), ('Sport', '26 - 165'), ('Ilves', '26 - 110'), ('Lukko', '119 - 192'), ('TPS', '86 - 137'), ('JYP', '28 - 75'), ('HPK', '32 - 74'), ('Kalpa', '11 - 53'), ('Jukurit', '136 - 159'), ('Ässät', '64 - 77'), ('Tappara', '23 - 28'), ('Pelicans', '120 - 45'), ('HIFK', '135 - 27'), ('KooKoo', '137 - 27'), ('Saipa', '157 - 15')]
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
# Actual output which autograder got - not sure why autograder didnt work
|
| 135 |
+
# Sorted by the goal difference:
|
| 136 |
+
('Ilves', '26 - 110')
|
| 137 |
+
('HPK', '32 - 74')
|
| 138 |
+
('KooKoo', '137 - 27')
|
| 139 |
+
('Ässät', '64 - 77')
|
| 140 |
+
('Tappara', '23 - 28')
|
| 141 |
+
('HIFK', '135 - 27')
|
| 142 |
+
('Saipa', '157 - 15')
|
| 143 |
+
('JYP', '28 - 75')
|
| 144 |
+
('Jukurit', '136 - 159')
|
| 145 |
+
('Kalpa', '11 - 53')
|
| 146 |
+
('Kärpät', '35 - 193')
|
| 147 |
+
('TPS', '86 - 137')
|
| 148 |
+
('Pelicans', '120 - 45')
|
| 149 |
+
('Sport', '26 - 165')
|
| 150 |
+
('Lukko', '119 - 192')
|