Given a 2D array of strings arr[][4], representing the scores of M football matches played in a tournament involving N teams, the task is to print the names of the teams in ascending order of their ranks.
- The rules of a match are as follows:
- Each team plays 2 matches.
- The winning team gets 2 points and the losing team gets 0.
- In the case of a tie, both teams share a point each.
- If GD, GA, and GF stand for Goal Difference, Goals Against, and Goals For respectively. The rank of a team is decided in the following priority order:
- Points > GD > GF > Lexicographical order of names.
Examples:
Input: arr[][] = { { “Spain”, “England”, “3″, “0″ }, { “England”, “France”, “1″, “1″ }, { “Spain”, “France”, “0”, “2” } }, N = 3, M = 3
Output: France Spain England
Explanation: Points Table after 3 matches apiece:
Teams Matches
PlayedGF GA GD Points Ranking Spain
2
3
2
1
2
2
England
2
1
4
-3
1
3
France
2
3
1
2
3
1
Input: arr[][] = { { “Spain”, “England”, “3″, “0″ }, { “England”, “France”, “1″, “1″ }, { “Spain”, “Spain”, “0”, “2” } }, N = 3, M = 3
Output: Invalid Input
Approach: The problem can be solved using sorting a dictionary by multiple attributes.
Follow the steps below to solve the problem:
- Traverse the list arr[][] and print “Invalid” if arr[i][0] is equal to arr[i][1] and break.
- Initialize a dictionary say table to store the GA, GF, and GD for a particular teams.
- Traverse the list arr[][] and perform the following operations:
- Increment GF for arr[i][0], table[arr[i][0]][0] by arr[i][2] and GA for the arr[i][0], table[arr[i][0]][1] by arr[i][3].
- Increment GF for arr[i][1], table[arr[i][1]][0] by arr[i][3] and GA for the arr[i][1], table[arr[i][1]][1] by arr[i][2].
- Update GD for arr[i][0], table[arr[i][0]] by table[arr[i][0][2] – table[arr[i][0][3].
- Update GD for arr[i][1], table[arr[i][1]] by table[arr[i][1][2] – table[arr[i][1][3].
- If arr[i][2] == arr[i][3], then increment table[arr[i][0][0] and table[arr[i][1]][0] both by 1.
- If arr[i][2] > arr[i][3], then increment table[arr[i][0][0] by 2.
- If arr[i][2] < arr[i][3], then increment table[arr[i][1][0] by 2.
- Now, sort the dictionary table based on the priority points > GD > GF and names as:
- table = sorted(table.items(), key=lambda r: (-r[1][0], -r[1][1], -r[1][2], r[0]))
- Finally, print the names of the teams in sorted table.
Below is the implementation of the above approach:
Python3
# Python program for the above approach # Function to find the ranking of teams def RankTeams(arr): # Traverse the list arr for i in range ( len (arr)): # arr[i][0] is equal to arr[i][1] if (arr[i][ 0 ] = = arr[i][ 1 ]): print ( "Invalid" ) return # Convert the goal to integer arr[i][ 2 ] = int (arr[i][ 2 ]) arr[i][ 3 ] = int (arr[i][ 3 ]) # Stores the list of GD, GA, and GF table = {} # Traverse the list for i in range ( len (arr)): # Store the list of GA, GF # and GD of first team li1 = [ 0 ] * 4 # Store the list of GA, GF # and GD of second team li2 = [ 0 ] * 4 # If arr[i][0] is in table if arr[i][ 0 ] in table: li1 = table[arr[i][ 0 ]] # If arr[i][1] is in table if arr[i][ 1 ] in table: li2 = table[arr[i][ 1 ]] # Increment GF by arr[i][2] li1[ 2 ] + = arr[i][ 2 ] # Increment GA by arr[i][3] li1[ 3 ] + = arr[i][ 3 ] # Increment GF by arr[i][3] li2[ 2 ] + = arr[i][ 3 ] # Increment GA by arr[i][2] li2[ 3 ] + = arr[i][ 2 ] # Update GD li1[ 1 ] = li1[ 2 ] - li1[ 3 ] li2[ 1 ] = li2[ 2 ] - li2[ 3 ] # If tie if (arr[i][ 2 ] = = arr[i][ 3 ]): li1[ 0 ] + = 1 li2[ 0 ] + = 1 # If arr[i][0] wins elif (arr[i][ 2 ] > arr[i][ 3 ]): li1[ 0 ] + = 2 # If arr[i][1] wins elif (arr[i][ 2 ] < arr[i][ 3 ]): li2[ 0 ] + = 2 # Update list in table table[arr[i][ 0 ]] = li1 table[arr[i][ 1 ]] = li2 # Traverse the sortd table in the given priority for key, value in sorted (table.items(), key = lambda r: ( - r[ 1 ][ 0 ], - r[ 1 ][ 1 ], - r[ 1 ][ 2 ], r[ 0 ])): # Print the team name print (key, end = '\n' ) # Driver Code # Input arr = [[ 'Spain' , 'England' , '3' , '0' ], [ 'England' , 'France' , '1' , '1' ], [ 'Spain' , 'France' , '0' , '2' ]] RankTeams(arr) |
France Spain England
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.