Answer to Question #191796 in Python for Ramya

Question #191796

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.


1
Expert's answer
2021-05-11T05:28:20-0400
def addResult(teams, name, result):
    L = teams.get(name, [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 printRes(teamRec):
    points, name, results = teamRec
    won, lost, draw = results
    played = won + lost + draw
    print(f'Team: {name}, MP: {played}, W: {won}, D: {draw}, L: {lost},  P: {points}')

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

    for _ 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:
        printRes(rec)

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