Skip to content

Instantly share code, notes, and snippets.

@jessestricker
Created January 7, 2023 21:20
Show Gist options
  • Save jessestricker/f353d2a050bc4d75670bf9630bd2b0e2 to your computer and use it in GitHub Desktop.
Save jessestricker/f353d2a050bc4d75670bf9630bd2b0e2 to your computer and use it in GitHub Desktop.
Calculates the awarded points for a football betting game.
from builtins import int, tuple, max
Result = tuple[int, int]
def points(game: Result, bet: Result) -> int:
"""
Calculates the awarded points for a given game result and bet.
- If the bet is exactly the same as the game result, 10 points are awarded.
- If the bet has the same outcome, between 7 and 1 points are awarded,
depending on the absolute difference between the goal differences.
- Otherwise, the bet has a different outcome and 0 points are awarded.
`Source: https://tippspiel.sueddeutsche.de`
:param game: The result of the game.
:param bet: The given bet for the game.
:return: The amount of points awarded.
"""
if bet == game:
return 10
goal_diff_game = game[0] - game[1]
goal_diff_bet = bet[0] - bet[1]
outcome_game = sign(goal_diff_game)
outcome_bet = sign(goal_diff_bet)
if outcome_game == outcome_bet:
return max(7 - abs(goal_diff_game - goal_diff_bet), 1)
else:
return 0
def sign(x: int) -> int:
if x > 0:
return 1
elif x == 0:
return 0
else:
return -1
def abs(x: int) -> int:
return x if x >= 0 else -x
#
# test cases
#
# correct bet
assert points((3, 2), (3, 2)) == 10
# correct outcome, no draw
assert points((1, 0), (2, 1)) == 7
assert points((1, 0), (2, 0)) == 6
assert points((1, 0), (3, 0)) == 5
assert points((1, 0), (5, 1)) == 4
assert points((1, 0), (6, 1)) == 3
assert points((1, 0), (7, 1)) == 2
assert points((1, 0), (7, 0)) == 1
assert points((1, 0), (8, 0)) == 1
# correct outcome, draw
assert points((1, 1), (2, 2)) == 7
assert points((1, 1), (5, 5)) == 7
# incorrect outcome
assert points((2, 1), (1, 2)) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment