Last active
October 30, 2023 15:42
-
-
Save prithajnath/a5e06c45cfbf2f47968f02d001966463 to your computer and use it in GitHub Desktop.
my_secret_santa_last_year.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # python3 my_secret_santa_last_year.py --number_of_users=10 | |
| from faker import Faker | |
| from uuid import uuid4 | |
| import itertools | |
| import argparse | |
| fake = Faker() | |
| def gen_pairs(perm): | |
| pairs = [] | |
| for i in range(len(users)): | |
| pairs.append((perm[i], perm[(i + 1) % len(users)])) | |
| return pairs | |
| users = [] | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| "--number_of_users", | |
| type=int, | |
| help="How many people are participating in the secret santa?", | |
| required=True, | |
| ) | |
| args = parser.parse_args() | |
| NUMBER_OF_USERS = args.number_of_users | |
| for i in range(NUMBER_OF_USERS): | |
| first_name = fake.first_name() | |
| last_name = fake.last_name() | |
| users.append(f"{first_name}{last_name}_{i+1}") | |
| all_possible_pairs = itertools.permutations(users) | |
| invalid_permutations = 0 | |
| for n, perm in enumerate(all_possible_pairs): | |
| if n == 0: | |
| first_perm = perm | |
| pairs = gen_pairs(first_perm) | |
| continue | |
| new_pairs = gen_pairs(perm) | |
| for pair in new_pairs: | |
| if pair in pairs: | |
| invalid_permutations += 1 | |
| break | |
| print( | |
| f"Found {invalid_permutations}/{n+1} invalid pairs. Thats {round(invalid_permutations/(n+1) * 100)}%!" | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment