Answer to Question #201260 in Python for sajid pasha

Question #201260

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.

sample 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-06-02T02:57:10-0400
def add_result(teams, name, result):
    if name in teams:
        status = teams[name]
    else:
        status = [0] * 3
    if result == 'win':
        status[0] += 1
    elif result == 'draw':
        status[1] += 1
    elif result == 'loss':
        status[2] += 1
    teams[name] = status


def calculate_points(name, results):
    return 3 * results[0] + results[1], name, results


def to_string(team):
    points, name, results = team
    won, draw, loss = results
    return f"Team: {name}, Matches Played: {won + draw + loss}, Won {won}, Lost {loss}, Draw: {draw}, Points {points}"


if __name__ == '__main__':
    n = int(input())
    teams = {}

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

    items = []
    for name in teams:
        items.append(calculate_points(name, teams[name]))

    items.sort(reverse=True)
    for item in items:
        print(to_string(item))

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