Skip to content

Instantly share code, notes, and snippets.

@shamilbi
Last active November 11, 2022 17:00
Show Gist options
  • Save shamilbi/02839e815459c0bce37601c5ecc252ec to your computer and use it in GitHub Desktop.
Save shamilbi/02839e815459c0bce37601c5ecc252ec to your computer and use it in GitHub Desktop.
jl.pyshop.ru: Задание 1. Разработать функцию определения счета в игре
class ScoreException(Exception):
pass
def get_score(game_stamps, offset):
'''
Takes list of game's stamps and time offset for which returns the scores for the home and away teams.
Please pay attention to that for some offsets the game_stamps list may not contain scores.
'''
def extract_scores(d: dict):
return ((d2 := d['score'])['home'], d2['away'])
try:
res = extract_scores(INITIAL_STAMP)
if not game_stamps or offset < 0:
return res
if offset > (d := game_stamps[-1])['offset']:
return extract_scores(d)
for d in game_stamps:
if d['offset'] > offset:
break
res = extract_scores(d)
# return home, away
return res
except (TypeError, KeyError) as e:
raise ScoreException() from e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment