Secret santa script
This commit is contained in:
commit
7ffedd972a
12
README.md
Normal file
12
README.md
Normal file
@ -0,0 +1,12 @@
|
||||
# Secret Santa
|
||||
|
||||
Example usage:
|
||||
|
||||
```
|
||||
$ ./secret-santa.py Elsa Anna Kristoffer Svein Olaf
|
||||
Svein -> Elsa
|
||||
Elsa -> Olaf
|
||||
Olaf -> Kristoffer
|
||||
Kristoffer -> Anna
|
||||
Anna -> Svein
|
||||
```
|
||||
19
secret-santa.py
Executable file
19
secret-santa.py
Executable file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import random
|
||||
|
||||
rng = random.SystemRandom()
|
||||
|
||||
def secret_santa(names):
|
||||
"""Draw pairings for secret santa.
|
||||
|
||||
We recognize the problem as a random Hamiltonian cycle
|
||||
on the complete graph formed by the set of names.
|
||||
"""
|
||||
g = list(names)
|
||||
rng.shuffle(g)
|
||||
return list(zip(g, g[1:] + g[:1]))
|
||||
|
||||
if __name__ == '__main__':
|
||||
for pair in secret_santa(sys.argv[1:]):
|
||||
print("%s -> %s" % pair)
|
||||
Loading…
x
Reference in New Issue
Block a user