From 7ffedd972ab11ae466a9404b062bcaa5caf40558 Mon Sep 17 00:00:00 2001 From: Michael Soukup Date: Sat, 8 Nov 2025 18:09:40 +0100 Subject: [PATCH] Secret santa script --- README.md | 12 ++++++++++++ secret-santa.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 README.md create mode 100755 secret-santa.py 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)