- 0
Notifications Mark All Read
No New notification
- Login
- Get Prime
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.
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)
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:
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
Login/Signup to comment