Java 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 Java program for Football league problems is the real-world problem the level of this question was much better.
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)
Table 1. Points Table after 3 matches
Team Name | Matches Played | Goals for | Goals Against | Goal Difference | Points | Ranking |
---|---|---|---|---|---|---|
Spain | 2 | 3 | 2 | 1 | 2 | 2 |
England | 2 | 1 | 4 | -3 | 1 | 3 |
France | 2 | 3 | 1 | 2 | 3 | 1 |
Italy | 0 | 0 | 0 | 0 | 0 | 4 |
Germany | 0 | 0 | 0 | 0 | 0 | 4 |
JAVA program for Football league problem
import java.util.Scanner; public class Main { public static void main(String[] args) { int n,m,i,j,k; int x=0; Scanner sc =new Scanner(System.in); System.out.println("\n\n"); System.out.println("Enter no of teams"); n = sc.nextInt(); String[] z = new String[n]; System.out.println("\n\n"); System.out.println("Enter names of Teams:"); for (i = 0; i < n; i++) { z[i]=sc.next(); } System.out.println("\n\n"); System.out.println("Enter no of matches"); m=sc.nextInt(); int[] pt = new int[n]; int[] gf = new int[n]; int[] ga = new int[n]; int[] gd = new int[n]; int[] mt = new int[n]; String[] s = new String[n+n]; String[][] a = new String[m][]; System.out.println("\n\n"); System.out.println("Enter details of matches (Team1 Team2 T1 T2goals)"); for (i = 0; i < m; i++) { for (j = 0; j < 4; j++) { a[i][j]=sc.next(); } System.out.println(); } for (i = 0; i < m; i++) { for (j = 0; j < 2; j++) { k=0; int temp=0; int index=0; while(((i-k)>=0)&&(i>0)) { if(a[i][j].equals(s[i-k])) { temp=1; index=i-k; } k++; } if(temp==1) { mt[index]+=1; gf[index]+=Integer.parseInt(a[i][(j+2)]); ga[index]+=Integer.parseInt(a[i][(3-j)]); if((Integer.parseInt(a[i][j+2]))>(Integer.parseInt(a[i][3-j]))) { pt[index]+=2; } else if(Integer.parseInt(a[i][j+2])==Integer.parseInt(a[i][3-j])) { pt[index]+=1; } else { pt[index]+=0; } } else { s[x]=a[i][j]; mt[x]+=1; gf[x]=Integer.parseInt(a[i][(j+2)]); ga[x]=Integer.parseInt(a[i][(3-j)]); if((Integer.parseInt(a[i][j+2]))>(Integer.parseInt(a[i][3-j]))) pt[x]=2; else if(Integer.parseInt(a[i][j+2])==Integer.parseInt(a[i][3-j])) pt[x]=1; else pt[x]=0; x++; } } } int v=m; int[] p = new int[n]; int b; int[] r = new int[n]; for (i = 0; i < m; i++) { for (j = 0; j < (m-i-1); ++j) { if(p[j]<p[j+1]) { b=p[j]; p[j]=p[j+1]; p[j+1]=b; } } int[] mp = new int[n]; int[] goalf = new int[n]; int[] goala = new int[n]; String tn[] = new String[n+n]; int[] g = null; for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if(pt[i]==p[j]) { tn[j]=s[j]; mp[j]=mt[i]; goalf[j]=gf[i]; goala[j]=ga[i]; goala[j]=ga[i]; g[j]=gd[i]; } } } for (i = m; i < n; i++) { mp[i]=0; goalf[i]=0; goala[i]=0; g[i]=0; p[i]=0; } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if((z[i].equals(tn[j]))) { z[i]="0"; } } } for (i = 0; i < n; i++) { if(z[i]!="0") { tn[v]=z[i]; v++; } } for (i = 0; i < n; i++) { if(p[i]==p[i+1]) { if(g[i]>g[i+1]) { r[i]=i+1; i=i+1; } else if(g[i]<g[i+1]) { r[i+1]=i+1; r[i]=i+2; i=i+1; } else { try { int d = tn[i].compareToIgnoreCase(tn[i+1]); if(d>0) { s[i]=tn[i]; tn[i]=tn[i+1]; tn[i+1]=s[i]; } } catch (Exception e) { e.printStackTrace(); } r[i]=i+1; r[i+1]=i+1; i=i+1; } } else r[i]=i+1; } System.out.println("\n\n\n\n"); System.out.println("\t\t\t"+"Football Points Table"+"\t\t"); System.out.println("\n\n"); System.out.println("Name"+"\t\t"+"Match"+"\t"+"GoalF"+"\t"+"GoalA"+"\t"+"GD"+"\t"+"Points"+"\t"+"Rank"); for (i = 0;i < n; i++) { System.out.println(tn[i]+"\t\t"+mp[i]+"\t"+goalf[i]+"\t"+goala[i]+"\t"+g[i]+"\t"+p[i]+"\t"+r[i]); } } } }
Football league Problem in few other Coding Languages
C
We don’t have the solution for this problem, you can contribute the answer of this code in C programming language, we post that answer on our page
C++
We don’t have the solution for this problem, you can contribute the answer of this code in C++ programming language, we post that answer on our page
Python
We don’t have the solution for this problem, you can contribute the answer of this code in C programming language, we post that answer on our page
here’s my py code..hope it may help…
def table(p,q):
if p==q:
return False
else:
return True
n=int(input())
l=list(input().split())
l.sort()
x=[0]*n
count=0
for i in range(3):
p,q,r,s=input().split()
if table(p,q)==True:
x[l.index(p)]+=int(r)
x[l.index(q)]+=int(s)
else:
count=1
print(‘Invalid Input’)
if count==0:
for i in range(len(x)-1):
for j in range(i+1,len(x)):
if x[i]ord(l[i+1][0]):
temp=l[i]
l[i]=l[i+1]
l[i+1]=temp
print(l)