Answer to Question #259056 in Python for Raju

Question #259056

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 terms 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 1

6

CSK;RR;loss

RR;DD;draw

MI;KKR;win

KKR;RR;loss

CSK;DD;draw

MI;DD; draw


Sample output 1

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-11-01T08:28:31-0400
# -*- coding: utf-8 -*-


def write_info(d, name, res):
    if name not in d:
        d[name] = {"mp":0,"w":0,"l":0,"d":0,"p":0}
    if res == "win":
        d[name]["w"] += 1
        d[name]["mp"] += 1
        d[name]["p"] += 3
    elif res == "draw":
        d[name]["d"] += 1
        d[name]["mp"] += 1
        d[name]["p"] += 1
    elif res == "loss":
        d[name]["l"] += 1
        d[name]["mp"] += 1
    return d


def print_info(d):
    for name in d:
        print(f'''Team: {name}, Matches Played: {d[name]["mp"]}, Won:  {d[name]["w"]}, Lost:  {d[name]["l"]},  Draw:  {d[name]["d"]},  Points:  {d[name]["p"]}''')
info = {}
n = int(input())
for i in range(n):
    s = input().split(";")
    if s[2] == "win":
        info = write_info(info, s[0], "win")
        info = write_info(info, s[1], "loss")
    elif s[2] == "loss":
        info = write_info(info, s[1], "win")
        info = write_info(info, s[0], "loss")
    elif s[2] == "draw":
        info = write_info(info, s[0], "draw")
        info = write_info(info, s[1], "draw")
print_info(info)

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