Write a program that reads all the match outcomes and summarizes the information of all the matches.
Points are given to the teams based on the outcome of the match.
A win earns a team 3 points. A draw earns 1. A loss earns 0.
The following information is expected:
MP: Matches Played
W: Matches Won
D: Matches Drawn (Tied)
L: Matches Lost
P: Points
The team information should be displayed in descending order of points.
def addResult(teams, name, result):
L = teams.get(name, [0,0,0])
if result == 'win':
L[0] += 1
elif result == 'draw':
L[1] += 1
else:
L[2] += 1
teams[name] = L
def calcPoints(name, results):
points = 3*results[0] + results[1]
return (points, name, results)
def printRes(teamRec):
points, name, results = teamRec
won, lost, draw = results
played = won + lost + draw
print(f'Team: {name}, MP: {played}, W: {won}, D: {draw}, L: {lost}, P: {points}')
def main():
n = int(input())
teams = {}
for _ in range(n):
line = input().split(';')
team1 = line[0].strip()
team2 = line[1].strip()
result = line[2].strip()
addResult(teams, team1, result)
if result == 'win':
result = 'loss'
elif result == 'loss':
result = 'win'
addResult(teams, team2, result)
L = []
for name in teams:
L.append(calcPoints(name, teams[name]))
L.sort(reverse=True)
for rec in L:
printRes(rec)
main()
Comments
Leave a comment