File size: 3,889 Bytes
8c60f91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
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.
'''





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