Answer to Question #225038 in Python for jayanth

Question #225038

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

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-08-10T17:40:54-0400
def addResult(teams, name, result):
    if name in teams:
        L = teams[name]
    else:
        L = [0, 0, 0]
    if result == 'win':
        L[0] += 1
    elif result == 'draw':
        L[1] += 1
    else:
        L[2] += 1
    teams[name] = L

def calcPoints(name, results):
    points = 3*results[0] + results[1]
    return (points, name, results)

def toString(teamRec):
    points, name, results = teamRec
    won, lost, draw = results
    played = won + lost + draw
    s = f'Team: {name}, Matches Played: {played}, Won: {won}, Lost: {lost}, Draw: {draw}, Points: {points}'
    return s

n = int(input()) 
teams = {}

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

L = []
for name in teams:
    L.append(calcPoints(name, teams[name]))

L.sort(reverse=True)

for rec in L:
    print(toString(rec))

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