Answer to Question #200891 in Python for vinay

Question #200891

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


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




1
Expert's answer
2021-05-30T18:33:34-0400
def addRecord(teams, name, result):
    if name not in teams:
        teams[name] = [0, 0, 0, 0]
    
    if result == 'win':
       teams[name][0] += 1
       teams[name][3] += 3
    elif result == 'draw':
        teams[name][1] += 1
        teams[name][3] += 1
    elif result == 'loss':
        teams[name][2] += 1


def main():
    n = int(input()) 
    teams = {}


    for _ in range(n):
        line = input()
        tokens = line.split(';')
        team1 = tokens[0].strip()
        team2 = tokens[1].strip()
        result = tokens[2].strip()
        addRecord(teams, team1, result)
        if result == 'win':
            result = 'loss'
        elif result == 'loss':
            result = 'win'
        addRecord(teams, team2, result)


    for team, results in sorted(teams.items(), key=lambda item: item[1][3], reverse=True):
        won, lost, draw, points = results
        played = won + lost +draw
        print(f'Team: {team}, MP: {played}, W: {won}, L: {lost}, D: {draw}, P: {points}')
   
main()

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