Answer to Question #203747 in Python for vinay

Question #203747

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.Input


The first line contains a single integer N, denoting the total no. of matches played.

The following N lines contain outcomes of N matches.

Each of those lines has information on the teams (T1, T2) which played and the outcome (O) in format T1;T2;O.

The outcome (O) is one of 'win', 'loss', 'draw' and refers to the first team listed.

See Sample Input/Output for better understanding.

The team name may contain spaces.





if input is zero ,the output will be "no output"


1
Expert's answer
2021-06-05T23:55:38-0400
totalNoMatchesPlayed=int(input())
if (totalNoMatchesPlayed==0):
	print('no output')
else:
    allTeams = {}
    for i in range(totalNoMatchesPlayed):
        tokens = input().split(';')
        t1 = tokens[0].strip()
        t2 = tokens[1].strip()
        matchOutcome = tokens[2].strip()
        if(t1 not in allTeams):
            allTeams[t1]=[0,0,0,0,0]
        if(t2 not in allTeams):
            allTeams[t2]=[0,0,0,0,0]
        if(t1 in allTeams and t2 in allTeams):
            allTeams[t1][0]+=1
            allTeams[t2][0]+=1


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


    for t in (sorted(allTeams.items(), key = lambda teamPoints: teamPoints[1][4],reverse=True)):
        print(f'Team: {t[0]}, ',end=' ')
        print(f'Matches Played: {t[1][0]}, ',end=' ')
        print(f'Won: {t[1][1]}, ',end=' ')
        print(f'Lost: {t[1][2]}, ',end=' ')
        print(f'Draw: {t[1][3]}, ',end=' ')
        print(f'Points: {t[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
New on Blog
APPROVED BY CLIENTS