commit 7ffedd972ab11ae466a9404b062bcaa5caf40558 Author: Michael Soukup Date: Sat Nov 8 18:09:40 2025 +0100 Secret santa script diff --git a/README.md b/README.md new file mode 100644 index 0000000..152b4ea --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/secret-santa.py b/secret-santa.py new file mode 100755 index 0000000..8c9df41 --- /dev/null +++ b/secret-santa.py @@ -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)