Answer to Question #258920 in Python for Villaan

Question #258920

IPL Match Details



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





Names of teams may contain spaces but will be less than 24 characters



100 >= N >= 0





Sample Input



6



CSK;RR;loss



RR;DD;draw



MI;KKR;win



KKR;RR;loss



CSK;DD;draw



MI;DD;draw





Sample Output



Team: RR, Matches Played: 3, Won: 2, Lost: 0, Draw: 1, Points: 7



Team: MI, Matches Played: 2, Won: 1, Lost: 0, Draw: 1, Points: 4



Team: DD, Matches Played: 3, Won: 0, Lost: 0, Draw: 3, Points: 3



Team: CSK, Matches Played: 2, Won: 0, Lost: 1, Draw: 1, Points: 1



Team: KKR, Matches Played: 2, Won: 0, Lost: 2, Draw: 0, Points: 0




1
Expert's answer
2021-10-30T13:32:02-0400
tm = {}
matches = int(input('Enter the number of matches played: '))


for m in range(matches):
    dmatch = input(f'Enter data for {m +1} m: ')
    t1, t2, outcome = dmatch.split(';')
    if t1 not in tm:
        tm[t1] = [0,0,0,0,0]
    if t2 not in tm:
        tm[t2] = [0,0,0,0,0]
        tm[t1][0] +=1
        tm[t2][0] += 1
    if outcome == 'win':
        tm[t1][1] += 1
        tm[t1][4] += 3
        tm[t2][3] += 1
    elif outcome == 'loss':
        tm[t2][1] += 1
        tm[t2][4] += 3
        tm[t1][3] += 1 
    elif outcome == 'draw':
        tm[t1][2] += 1
        tm[t2][2] += 1
        tm[t1][4] += 1
        tm[t2][4] += 1


tlist=sorted(tm.items(),key=lambda t:t[1][4],reverse=True) 
for i in tlist:
    print(f'Team: {i[0]}')
    print(f'MP: {i[1][0]}')
    print(f'W: {i[1][1]}')
    print(f'D: {i[1][2]}')
    print(f'L: {i[1][3]}')
    print(f'P: {i[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