Answer to Question #235650 in Python for rasi

Question #235650

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:

6

CSK;RR;loss

RR;DD;draw

MI;KKR;win

KKR;RR;loss

CSK;DD;draw

MI;DD;draw


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-09-10T18:53:25-0400
def IPL():
    N=int(input("Enter an Integer: "))
    
    if N>=0 and N<=100:
        teams = {}
        for i in range(N):
            val = input().split(';')
            first = val[0].strip()
            second= val[1].strip()
            result = val[2].strip()
            #adding teams to the teams dictionary created above"
            if(first not in teams):
                #Matches Played=0, Won=0, Lost=0, Draw=0, Points=0
                teams[first]=[0,0,0,0,0]
            if(second not in teams ):
                teams [second ]=[0,0,0,0,0]        
            if(first  in teams  and second  in teams ):
                teams[first ][0]+=1
                teams[second ][0]+=1
                #Extracting winner
                if result == 'win':
                    teams[first][1]+=1
                    teams[second][2]+=1
                elif result == 'loss':
                    teams[first][2]+=1
                    teams[second][1]+=1
                elif result == 'draw':
                    teams[first][3]+=1
                    teams[second][3]+=1




        
        for team in teams:
            teams[team][4]=teams[team][1]*3+teams[team][3]
    
    teams=(sorted(teams.items(), key = lambda points: points[1][4],reverse=True))
   
    for team in teams:
        print("Team: "+str(team[0])+", Matches Played: "+str(team[1][0])+", Won: "+str(team[1][1])+", Lost: "+str(team[1][2])+", Draw: "+str(team[1][3])+", Points:  "+str(team[1][4]))
    
      
        
IPL()                
            
     

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