Answer to Question #234248 in Python for rasi

Question #234248

Write a program that reads all the match outcomes and summarizes the information of all the matches. A win earns a team 3 points. The team information should be displayed in descending order of points.

Input1-The first line contains a single integer N, denoting the total no. of matches played.

The following N lines contain outcomes of N matches. The team name may contain spaces.

6

CSK;RR;loss

RR;DD;draw

MI;KKR;win

KKR;RR;loss

CSK;DD;draw

MI;DD;draw

Output1- If there are no teams to print in summary, print "No Output". Constraints

Names of teams may contain spaces but will be less than 24 characters 100 >= N >= 0.

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-09-09T23:46:35-0400
team = {}
matches = int(input('Enter the number of matches played: '))
# data input
for match in range(matches):
    data_match = input(f'Enter data for {match +1} match: ')
    team_1, team_2, outcome = data_match.split(';')
    if team_1 not in team:
        team[team_1] = [0,0,0,0,0]
    if team_2 not in team:
        team[team_2] = [0,0,0,0,0]
    team[team_1][0] +=1
    team[team_2][0] += 1
    if outcome == 'win':
        team[team_1][1] += 1
        team[team_1][4] += 3
        team[team_2][3] += 1
    elif outcome == 'loss':
        team[team_2][1] += 1
        team[team_2][4] += 3
        team[team_1][3] += 1 
    elif outcome == 'draw':
        team[team_1][2] += 1
        team[team_2][2] += 1
        team[team_1][4] += 1
        team[team_2][4] += 1
# data output
team_list=sorted(team.items(),key=lambda t:t[1][4],reverse=True) 
for item in team_list:
    print(f'Team: {item[0]}')
    print(f'MP: {item[1][0]}')
    print(f'W: {item[1][1]}')
    print(f'D: {item[1][2]}')
    print(f'L: {item[1][3]}')
    print(f'P: {item[1][4]}')

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