Files
Lottery_Project/util/odds.py
T

83 lines
2.8 KiB
Python

"""
util/odds.py
Calculate the odds of winning a lottery game
"""
from math import comb
def calculate_odds(pool_size: int, draw_count: int) -> int:
"""
Calculate the total number of possible combinations in a set of numbers
with a specific draw count.
:param pool_size: Total count of numbers in the pool
:param draw_count: The count of numbers to draw from the pool
:return: The total number of possible combinations
Example:
If a lottery game has a main ball pool of 49 numbers from which 5
numbers are drawn and a pool of
18 numbers from which 1 number is drawn (game ball), calculate the
odds of winning that game.
>>> calculate_odds(pool_size=49, draw_count=5)
1906884
>>> calculate_odds(pool_size=18, draw_count=1)
18
"""
if draw_count < 0 or draw_count > pool_size:
raise ValueError(
'Integer values must be positive and draw counts must be less '
'than pool size.',
)
total_combinations = comb(pool_size, draw_count)
return total_combinations
def get_odds_of_winning(
main_ball_draw_pool: int,
main_ball_draw_count: int,
game_ball_draw_pool: int,
game_ball_draw_count: int,
) -> str:
"""
Calculate the odds of winning a specific lottery game with one draw
:param main_ball_draw_pool: Total count of numbers in the main ball pool
:param main_ball_draw_count: The count of numbers to draw from the
main ball pool
:param game_ball_draw_pool: Total count of numbers in the game ball pool
:param game_ball_draw_count: The count of numbers to draw from the
game ball pool
:return: The odds of winning a specific lottery game with one draw
Example:
To calculate the odds of winning a lottery game with 5 numbers
drawn from a main ball pool of 49 and
1 number drawn from a game ball pool of 19 numbers.
>>> get_odds_of_winning(main_ball_draw_pool=49,
main_ball_draw_count=5, game_ball_draw_pool=18,
game_ball_draw_count=1)
'The odds of winning are 1 in 34,323,912'
"""
if (
main_ball_draw_count < 0 or main_ball_draw_count >
main_ball_draw_pool or
game_ball_draw_count < 0 or game_ball_draw_count >
game_ball_draw_pool):
raise ValueError(
'Integer values must be positive and draw counts must be less '
'than pool size.',
)
main_ball_odds = calculate_odds(main_ball_draw_pool, main_ball_draw_count)
game_ball_odds = calculate_odds(game_ball_draw_pool, game_ball_draw_count)
return f"The odds of winning are 1 in {main_ball_odds * game_ball_odds:,}"