Answer to Question #186148 in Python for hemanth

Question #186148

IPL Match Details


Write a program that reads all match outcomes and summarizes information of all matches.

Points are given to teams based on outcome of 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


team information should be displayed in descending order of points.






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


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-04-29T05:41:19-0400
def write_result(name_comand, result, results_list):


	if len(results_list) > 0:
		for i in range(len(results_list)):
			if results_list[i][0] == name_comand:
				results_list[i][1] = [a + b for a, b in zip(results_list[i][1], result)]
				return results_list
	results_list.append([name_comand, result])
	return results_list


while True:
	try:
		games = int(input('games played: '))
		if games < 0:
			print('must be an integer greater than or equal to zero')
			continue
	except ValueError:
		print('must be an integer greater than or equal to zero')
		continue
	matches_score = list()
	while games > 0:
		tmp = input().split(sep=';')
		if len(tmp) != 3 or tmp[2] not in ['loss', 'draw', 'win']:
			print('incorrect input. must be \"first team;second team;result\"')
		else:
			matches_score.append(tmp)
			games -= 1
	break
res = list()
w = [1, 1, 0, 0, 3]
l = [1, 0, 1, 0, 0]
d = [1, 0, 0, 1, 1]
for i in range(len(matches_score)):
	if matches_score[i][2] == 'win':
		res = write_result(matches_score[i][0], w, res)
		res = write_result(matches_score[i][1], l, res)
	elif matches_score[i][2] == 'loss':
		res = write_result(matches_score[i][0], l, res)
		res = write_result(matches_score[i][1], w, res)
	else:
		res = write_result(matches_score[i][0], d, res)
		res = write_result(matches_score[i][1], d, res)
res.sort(key=lambda i: i[1][4], reverse=True)
if len(res) == 0:
	print('No Output')
else:
	for el in res:
		print('Team: {}, Matches Played: {}, Won: {}, Lost: {}, Draw: {}, Points: {}'.format(el[0],*el[1]))

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