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