Python Program for Football league problem

Football league problem

This problem was asked in TCS CodeVita. TCS CodeVita is an online coding programming competition conducted by Tata Consultancy Services to bring out the best talent in the programming world. This Python program for Football league problems is the real-world problem the level of this question was much better.

Python program to check whether two numbers are Friendly Pair

                                                                                 Problem Statement

Football League Table Statement : All major football leagues have big league tables. Whenever a new match is played, the league table is updated to show the current rankings (based on Scores, Goals For (GF), Goals Against (GA)). Given the results of a few matches among teams, write a program to print all the names of the teams in ascending order (Leader at the top and Laggard at the bottom) based on their rankings.

Rules:
A win results in 2 points, a draw results in 1 point an
d a loss is worth 0 points. The team with the most goals in a match wins the match. Goal Difference (GD) is calculated as Goals For (GF) Goals Against (GA). Teams can play a maximum of two matches against each other Home and Away matches respectively.

The ranking is decided as follows: Team with maximum points is ranked 1 and minimum points is placed last Ties are broken as follows Teams with same points are ranked according to Goal Difference(GD).

If Goal Difference(GD) is the same, the team with higher Goals For is ranked ahead

If GF is same, the teams should be at the same rank but they should be printed in case-insensitive alphabetic according to the team names. More than 2 matches of same teams, should be considered as Invalid Input.

A team can’t play matches against itself, hence if team names are same for a given match, it should be considered Invalid Input

Input Format:
First line of input will contain number of teams (N) Second line contains names of the teams (Na) delimited by a whitespace character Third line contains number of matches (M) for which results are available Next M lines contain a match information tuple {T1 T2 S1 S2}, where tuple is comprised of the following information

  • T1 Name of the first team
  • T2 Name of the second team
  • S1 Goals scored by the first team
  • S2 Goals scored by the second team

Output Format:
Team names in order of their rankings, one team per line OR Print “Invalid Input” where appropriate.

Constraints:

0< N <=10,000 0<=S1,S2

Example:
Consider 5 teams Spain, England, France, Italy and Germany with the following fixtures:

  • Match 1: Spain vs. England (2-0) (Spain gets 2 points, England gets 0)
  • Match 2: England vs. France (1-1) (England gets 1 point, France gets 1)
  • Match 3: Spain vs. France (0-2) (Spain gets 0 points, France gets 2)
Since, Italy and Germany are tied for points, goals difference is checked. Both have same, so, Goals For is checked. Since both are same. Germany and Italy share the 4th rank. Since Germany appears alphabetically before Italy, Germany should be printed before Italy. Then the final result is: France Spain England Germany Italy
Sample Input 1:
5
Spain England France Italy Germany
Spain England 3 0
England France 1 1
Spain France 0 2
Sample Output 1:
France
Spain
England
Germany
Italy
Sample Input 2:
5
Spain England France Italy Germany
Spain England 3 0
England France 1 1
Spain Spain 0 2
Sample Output 2:
Invalid Input

Implementation:

  • Take the name as input and store in a dictionary name as key and integer (0,1..n) as value.
  • Construct a 2D array of size n*5 (ar[n][5]) where ith row will have the information about the key in the dictionary having value ‘i’.
  • Coloumns of array will be match played, GF , GA , GD , points.
  • Add all the points in a dictionary with same ith row value as key.
  • Sort the dictionary with points.
  • Add the name of the country in a list according to their rank and latter sort them if points are equal and GD is diffrent or according to their name. 

Python Code:

n=int(input()) #number of the country 
nt=list(map(str,input().split())) #name of the country
na=dict() 
for i in range(n):
    na[nt[i]]=i  #na[‘spain]=0 , na[‘England]=1
    
del nt    
m=int(input()) #number of matches 
ar=[[0 for i in range(5)] for j in range(n)] #array for storing the the no of match , GF , GA , GD POINTS 
po=dict() #DICTIONARY FOR POINTS
fla=1
mt=dict() #for keeping count that no two country play more than 2 match with each other
for i in range(m):
    t1,t2,g1,g2=input().split(” “)
    if(t1<t2):   
        z=t1+t2
        if(t1+t2 not in mt):
            mt[t1+t2]=1  # matches played  between two country arranger alphabetically like FranceSpain  not as SpainFrance
        else:
            mt[t1+t2]+=1 
    else:
        z=t2 + t1
        if(t2+t1 not in mt):
            mt[t2+t1]=1 
        else:
            mt[t2+t1]+=1
        
    if(t1==t2 or mt[z]>2): #if match between two team is greater than 2 or playing with themself break
        print(‘Invalid input’)
        fla=0
        break
    g1=int(g1)  
    g2=int(g2)
    x=na[t1] #ith row value to be used for maintaing the table
    y=na[t2]
    ar[x][0]+=1  #match played
    ar[y][0]+=1
    #Gf
    ar[x][1]+=g1
    ar[y][1]+=g2 
    #GA
    ar[x][2]+=g2 
    ar[y][2]+=g1 
    #GD
    ar[x][3]=ar[x][1]-ar[x][2]
    ar[y][3]=ar[y][1]-ar[y][2
    #points
    if(g1>g2):
        ar[x][4]+=2
        
    elif(g1==g2):
        ar[x][4]+=1 
        ar[y][4]+=1
        
    else:
        ar[y][4]+=2 
if(fla==1): #if valid input
    del mt
    for i in range(n):
        po[i]=ar[i][4#point dictionary 
     #sort the dictionary a/c to their value    
    sorted_dict = {}
    sorted_keys = sorted(po, key=po.get)  # [1, 3, 2]
    
    for w in sorted_keys:
        sorted_dict[w] = po[w] #sorted dict
    #print(*ar,sep=’\n’) 
    #print(sorted_dict)
    del po
    ky=sorted_dict.keys()
    del sorted_keys
    li=[]  
    prev=-1
    prep=0
    val=list(na.values()) #list of value of dict(na)
    kyl=list(na.keys())   #list of key of dict(na)
    #storing the name of the country a/c to their point
    for j in (ky):
        x=val.index(j)
        li.append(kyl[x]) #appending their name
    #print(li) 
    #print(na) 
    #sorting the list with same point but diffrent GD or same point and same GD then alphabetically.
    for i in range(len(li)): 
        x=li[i]
        y=na[x]
        #print( x)
        for j in range(i+1,len(li)):
            a=li[j]
            b=na[a]
            #print( a)
            if(ar[y][4]==ar[b][4and ar[y][3]>ar[b][3]): #if point is same bt GD is diffrent
                #print(‘h’)
                li[i],li[j]=li[j],li[i]
            elif(ar[y][4]==ar[b][4and ar[y][3]==ar[b][3]): # point and GD both are same
                if(x<a): #then according to their name
                    #print(‘j’)
                    li[i],li[j]=li[j],li[i]
                    #print(li)
    li.reverse()
    print(*li,sep=\n)
        
Input:
5
Spain England France Italy Germany
3
Spain England 3 0
England France 1 1
Spain France 0 2 

Output :
France
Spain
England
Germany
Ital
Input :

5
Spain England France Italy Germany
3
Spain England 3 0
England France 1 1
Spain Spain 0 2

Output :

Invalid input