Answer to Question #235736 in Python for dnyanesh

Question #235736

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 output should contain summarized information of all the matches in a format similar to 'Team: CSK, Matches Played: 4, Won: 2, Lost: 1, Draw: 1, Points: 7' for each team in a new line.

If there are no teams to print in summary, print "No Output".


1
Expert's answer
2021-09-10T12:16:58-0400
N=int(input())
if (N==0):
	print('No Output')
else:
    teams = {}
    for i in range(N):
        values = input().split(';')
        t1 = values[0].strip()
        t2 = values[1].strip()
        matchOutcome = values[2].strip()
        if(t1 not in teams):
            teams[t1]=[0,0,0,0,0]
        if(t2 not in teams):
            teams[t2]=[0,0,0,0,0]
        if(t1 in teams and t2 in teams):
            teams[t1][0]+=1
            teams[t2][0]+=1


        if matchOutcome == 'win':
            teams[t1][1]+=1
            teams[t2][2]+=1
        elif matchOutcome == 'loss':
            teams[t1][2]+=1
            teams[t2][1]+=1
        elif matchOutcome == 'draw':
            teams[t1][3]+=1
            teams[t2][3]+=1
    for team in teams:
        teams[team][4]=teams[team][1]*3+teams[team][3]


    for team in (sorted(teams.items(), key = lambda teamPoints: teamPoints[1][4],reverse=True)):
        print(f'Team: {team[0]}, ',end=' ')
        print(f'Matches Played: {team[1][0]}, ',end=' ')
        print(f'Won: {team[1][1]}, ',end=' ')
        print(f'Lost: {team[1][2]}, ',end=' ')
        print(f'Draw: {team[1][3]}, ',end=' ')
        print(f'Points: {team[1][4]}')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS