File size: 5,441 Bytes
8c60f91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bade301
 
8c60f91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bade301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
'''
Write a function

sortby_goalsdiff(teams: list)

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

("TPS", "201 - 51")

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). 

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.

Note! You can either use a lambda statement or write your own value function. This does not affect the scoring.
'''



#########################
# approach 1
def sortby_goalsdiff(teams: list):
    def get_goal_difference(team):
        goals_for, goals_against = map(int, team[1].split(' - '))
        return goals_for - goals_against

    return sorted(teams, key=get_goal_difference)

# Example1:
teams = [
('Kalpa', '53 - 58')
,('Saipa', '144 - 21')
,('Tappara', '37 - 17')
,('Ilves', '16 - 121')
,('HIFK', '124 - 121')
,('Pelicans', '86 - 49')
,('Sport', '180 - 48')
,('KooKoo', '35 - 87')
,('HPK', '130 - 52')
,('Kärpät', '58 - 15')
,('Jukurit', '34 - 133')
,('Lukko', '195 - 10')
,('JYP', '107 - 45')
,('Ässät', '98 - 170')
,('TPS', '197 - 109')
]


print(sortby_goalsdiff(teams))
[('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')]














# Example2
# Autograder input
# Original order:
('Ilves', '26 - 110')
('HPK', '32 - 74')
('KooKoo', '137 - 27')
('Ässät', '64 - 77')
('Tappara', '23 - 28')
('HIFK', '135 - 27')
('Saipa', '157 - 15')
('JYP', '28 - 75')
('Jukurit', '136 - 159')
('Kalpa', '11 - 53')
('Kärpät', '35 - 193')
('TPS', '86 - 137')
('Pelicans', '120 - 45')
('Sport', '26 - 165')
('Lukko', '119 - 192')






# Expected output
# Sorted by the goal difference:
('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')




# Actual output by my python Terminal
teams = [
('Ilves', '26 - 110')
,('HPK', '32 - 74')
,('KooKoo', '137 - 27')
,('Ässät', '64 - 77')
,('Tappara', '23 - 28')
,('HIFK', '135 - 27')
,('Saipa', '157 - 15')
,('JYP', '28 - 75')
,('Jukurit', '136 - 159')
,('Kalpa', '11 - 53')
,('Kärpät', '35 - 193')
,('TPS', '86 - 137')
,('Pelicans', '120 - 45')
,('Sport', '26 - 165')
,('Lukko', '119 - 192')
]
print(sortby_goalsdiff(teams))
[('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')]





# Actual output which autograder got - not sure why autograder didnt work
# Sorted by the goal difference:
('Ilves', '26 - 110')
('HPK', '32 - 74')
('KooKoo', '137 - 27')
('Ässät', '64 - 77')
('Tappara', '23 - 28')
('HIFK', '135 - 27')
('Saipa', '157 - 15')
('JYP', '28 - 75')
('Jukurit', '136 - 159')
('Kalpa', '11 - 53')
('Kärpät', '35 - 193')
('TPS', '86 - 137')
('Pelicans', '120 - 45')
('Sport', '26 - 165')
('Lukko', '119 - 192')

############################


# approach 2

def sortby_goalsdiff(teams: list) -> list:
    """
    Sort teams based on their goal difference in ascending order.
    
    Parameters:
        teams: List of tuples, where each tuple contains:
               (team_name: str, goals_for - goals_against: str)
    
    Returns:
        List of team tuples sorted by their goal difference in ascending order.
    """
    
    # Define a helper function to calculate goal difference
    def get_goal_difference(team):
        goals_for, goals_against = map(int, team[1].split(' - '))
        return goals_for - goals_against


    # Sort the teams list INPLACE using the goal difference as the key
    teams.sort(key=get_goal_difference)
    # Return the sorted list
    return teams






Original order:
('Kalpa', '123 - 61')
('Pelicans', '99 - 151')
('Tappara', '29 - 46')
('Ilves', '179 - 56')
('Jukurit', '32 - 60')
('JYP', '108 - 112')
('Lukko', '147 - 115')
('Ässät', '37 - 35')
('Kärpät', '179 - 165')
('Saipa', '78 - 102')
('TPS', '197 - 164')
('KooKoo', '89 - 132')
('HIFK', '48 - 136')
('HPK', '164 - 175')
('Sport', '168 - 153')


Sorted by the goal difference:
('HIFK', '48 - 136')
('Pelicans', '99 - 151')
('KooKoo', '89 - 132')
('Jukurit', '32 - 60')
('Saipa', '78 - 102')
('Tappara', '29 - 46')
('HPK', '164 - 175')
('JYP', '108 - 112')
('Ässät', '37 - 35')
('Kärpät', '179 - 165')
('Sport', '168 - 153')
('Lukko', '147 - 115')
('TPS', '197 - 164')
('Kalpa', '123 - 61')
('Ilves', '179 - 56')