Answer to Question #175160 in Python for Ravi

Question #175160

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

see sample output for better understanding.

The team information should be displayed in descending order of points.Input.


100 >= N >= 0 HINT:you can maintain all match outcomes in dictionaries.


Sample Input

6

CSK;RR;loss

RR;DD;draw

MI;KKR;win

KKR;RR;loss

CSK;DD;draw

MI;DD;draw


Sample 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-03-25T16:59:01-0400
input_list = []
while True:
    input_str = input()
    if input_str == "":
        break
    else:
        input_list.append(input_str)
# print(input_list)

namescores = {}
for line in input_list:
    line_split = line.split(':')
    name1 = line_split[0]
    name2 = line_split[1]
    namescores[name1] = {}
    namescores[name2] = {}
    namescores[name1]['3set_game_won'] = 0
    namescores[name1]['5set_game_won'] = 0
    namescores[name2]['3set_game_won'] = 0
    namescores[name2]['5set_game_won'] = 0
    namescores[name1]['set_won'] = 0
    namescores[name1]['set_lost'] = 0
    namescores[name2]['set_won'] = 0
    namescores[name2]['set_lost'] = 0
    namescores[name1]['game_won'] = 0
    namescores[name1]['game_lost'] = 0
    namescores[name2]['game_won'] = 0
    namescores[name2]['game_lost'] = 0
# print(str(namescores))

for line in input_list:
    line_split = line.split(':')
    name1 = line_split[0]
    name2 = line_split[1]
    scores = line_split[2]
    score_pairs = scores.split(',')
    name1_3set_won = 0
    name1_5set_won = 0
    name2_3set_won = 0
    name2_5set_won = 0

    for score_pair in score_pairs:

        score_pair_split = score_pair.split('-')
        namescores[name1]['game_won'] = int(namescores[name1]['game_won']) + int(score_pair_split[0])
        namescores[name1]['game_lost'] = int(namescores[name1]['game_lost']) + int(score_pair_split[1])
        namescores[name2]['game_won'] = int(namescores[name2]['game_won']) + int(score_pair_split[1])
        namescores[name2]['game_lost'] = int(namescores[name2]['game_lost']) + int(score_pair_split[0])

        if (len(score_pairs) > 3 and len(score_pairs) <= 5 and (
                int(score_pair_split[0]) >= 6 or int(score_pair_split[1]) >= 6)):

            if int(score_pair_split[0]) > int(score_pair_split[1]):
                namescores[name1]['set_won'] = int(namescores[name1]['set_won']) + 1
                namescores[name2]['set_lost'] = int(namescores[name2]['set_lost']) + 1
                name1_5set_won += 1

            elif int(score_pair_split[0]) < int(score_pair_split[1]):
                namescores[name1]['set_lost'] = int(namescores[name1]['set_lost']) + 1
                namescores[name2]['set_won'] = int(namescores[name2]['set_won']) + 1
                name2_5set_won += 1

        elif (len(score_pairs) <= 3 and (int(score_pair_split[0]) >= 6 or int(score_pair_split[1]) >= 6)):

            if int(score_pair_split[0]) > int(score_pair_split[1]):
                namescores[name1]['set_won'] = int(namescores[name1]['set_won']) + 1
                namescores[name2]['set_lost'] = int(namescores[name2]['set_lost']) + 1
                name1_3set_won += 1

            elif int(score_pair_split[0]) < int(score_pair_split[1]):
                namescores[name1]['set_lost'] = int(namescores[name1]['set_lost']) + 1
                namescores[name2]['set_won'] = int(namescores[name2]['set_won']) + 1
                name2_3set_won += 1

    # print(name1_5set_won)
    # print(name2_5set_won)
    # print(name1_3set_won)
    # print(name2_3set_won)
    if (name1_5set_won >= (name2_5set_won + 1)):
        namescores[name1]['5set_game_won'] = int(namescores[name1]['5set_game_won']) + 1

    elif (name2_5set_won >= (name1_5set_won + 1)):
        namescores[name2]['5set_game_won'] = int(namescores[name2]['5set_game_won']) + 1

    elif (name1_3set_won >= (name2_3set_won + 1)):
        namescores[name1]['3set_game_won'] = int(namescores[name1]['3set_game_won']) + 1

    elif (name2_3set_won >= (name1_3set_won + 1)):
        namescores[name2]['3set_game_won'] = int(namescores[name2]['3set_game_won']) + 1

# print(namescores)

final_list = [[] for name in namescores]
i = 0
for player in namescores:
    final_list[i].append(player)
    final_list[i].append(namescores[player]['5set_game_won'])
    final_list[i].append(namescores[player]['3set_game_won'])
    final_list[i].append(namescores[player]['set_won'])
    final_list[i].append(namescores[player]['game_won'])
    final_list[i].append(namescores[player]['set_lost'])
    final_list[i].append(namescores[player]['game_lost'])
    i += 1

final_list.sort(key=lambda x: (x[1], x[2], x[3], x[4], x[6], x[5]))
final_list = final_list[::-1]
# print(final_list)

for i in range(len(final_list)):
    print(final_list[i][0], end=" ")
    print(final_list[i][1], end=" ")
    print(final_list[i][2], end=" ")
    print(final_list[i][3], end=" ")
    print(final_list[i][4], end=" ")
    print(final_list[i][5], end=" ")
    print(final_list[i][6], end="\n")

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