-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecret Santa
More file actions
32 lines (29 loc) · 1.14 KB
/
Secret Santa
File metadata and controls
32 lines (29 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import random
def secret_santa(names):
"""
Given a list of names, randomly match each person with another person for Secret Santa.
Returns a dictionary where the keys are the names and the values are the names of the people they will give a gift to.
"""
### create a list
participants = list(names)
### shuffle the list, all of the lists with the same names will be diferents
random.shuffle(participants)
### create a dictionary
matchings = {}
for i in range(len(participants)):
matchings[participants[i]] = participants[(i + 1) % len(participants)]
return matchings
if __name__ == '__main__':
### block at the bottom of the program is run only if the program is executed directly (as opposed to being imported as a module)
### Example usage:
names = []
while True:
name = input("Enter a name (or leave blank to stop): ")
if name:
names.append(name)
else:
break
matchings = secret_santa(names)
print("Secret Santa matchings:")
for name, recipient in matchings.items():
print(f"{name} will give a gift to {recipient}.")