https://github.com/benedekfazekas/secret-santa
find a derangement
https://github.com/benedekfazekas/secret-santa
Last synced: 3 months ago
JSON representation
find a derangement
- Host: GitHub
- URL: https://github.com/benedekfazekas/secret-santa
- Owner: benedekfazekas
- Created: 2013-12-15T20:56:45.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-12-15T21:54:12.000Z (over 11 years ago)
- Last Synced: 2025-01-19T13:34:18.859Z (4 months ago)
- Language: Clojure
- Homepage:
- Size: 102 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Secret Santa
============helps you generate random pairs in a set where no element is paired with itself
# background
you know what the background is. your friends are coming over for an xmas dinner and you want to generate random pairs the week before to encourage some nice interaction withing the group (giving presents) after the dinner. should be easy.
turns out it is not that easy after all. at least finding a [derangement](http://en.wikipedia.org/wiki/Derangement) is not trivial.
# implementation
is rather brute force. according to this [blog post](http://cameroncounts.wordpress.com/2013/01/18/finding-derangements-without-cfsg/) there is a much more elegant algorithm. well, there is always room for improvement ;)
anyway current implementation should take
```
n! - ¡n + 1
```
worst case. There is no validation of the input either. Call it like:
```clojure
(pair-up #{:a :b :c :d :e :f :g})
```
or even
```clojure
(pair-up #{"fred" "virag" "ceri" "benedek" "matruska" "regi" "pedjar"})
```
the result should be something like:
```
(("virag" "regi")
("matruska" "ceri")
("fred" "matruska")
("pedjar" "virag")
("benedek" "pedjar")
("regi" "benedek")
("ceri" "fred"))
```