From a8e95bba210e660a0dc0abd0ee37a19a27058245 Mon Sep 17 00:00:00 2001 From: User Date: Sun, 4 May 2025 11:38:42 +0300 Subject: [PATCH] First code --- Парадокс ДР/main.py | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Парадокс ДР/main.py diff --git a/Парадокс ДР/main.py b/Парадокс ДР/main.py new file mode 100644 index 0000000..eb12241 --- /dev/null +++ b/Парадокс ДР/main.py @@ -0,0 +1,70 @@ +import datetime, random + + +def getBirthday(numberOfBerthday): + birthdays = [] + for i in range(numberOfBerthday): + start_of_year = datetime.date(2000, 1, 1) + randomNumber_of_Days = datetime.timedelta(random.randint(1, 364)) + birthday = start_of_year + randomNumber_of_Days + birthdays.append(birthday) + return birthdays + +def getMatch(birthdays): + if len(birthdays) == len(set(birthdays)): + return None + + for a, birthdayA in enumerate(birthdays): + for b, birthdayB in enumerate(birthdays[a + 1:]): + if birthdayA == birthdayB: + return birthdayA + +# Создаем картеж +months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', + 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec') + +while True: + """Ввод пользователя""" + print("How many birthdays shall i generate? (Max 100)") + """Добавь потом в сам цикл while""" + responses = input("> ") + if responses.isdecimal() and (0 < int(responses) <= 100): + numBDay = int(responses) + break + +print("Here are ", responses, " birthdays:") +birthdays = getBirthday(numBDay) +for i ,birthday in enumerate(birthdays): + if i != 0: + print(', ', end='') + montName = months[birthday.month - 1] + dateText = f"{montName} {birthday.day}" + print(dateText, end='') + +print() +match = getMatch(birthdays) + +print("In this simulations, ", end='') +if match != None: + montName = months[match.month - 1] + dateText = f"{montName} {match.day}" + print(f'multiple peple have a birthday on {dateText}') +else: + print("There are no matching birthdays.") + +print("Lets run anuther 100_000 simulater ") + +simMatch = 0 +for i in range(100_000): + if i % 10_000 == 0: + print(i , 'simulations run ...') + birthdays = getBirthday(numBDay) + if getMatch(birthdays) != None: + simMatch += 1 +print("100_000 simulations run.") + +probability = round((simMatch / 100_000) * 100, 2) +print(f"Out jf 100_000 simulations of {numBDay} peple, there was a\n" + f"matching birthday in tht group {simMatch} times. This means\n" + f"that {numBDay} peple have a {probability} % chanse jf\n" + "having a matching birthday in their group.\n")