Skip to content

Instantly share code, notes, and snippets.

@sumitsaiwal
Created June 17, 2020 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sumitsaiwal/27ec6a9052951bb29ff0d2a26c08e31d to your computer and use it in GitHub Desktop.
Save sumitsaiwal/27ec6a9052951bb29ff0d2a26c08e31d to your computer and use it in GitHub Desktop.
Tennis Scoreboard Problem in Python.
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment