Secret santa algorithm

前端 未结 9 1424
温柔的废话
温柔的废话 2020-12-04 22:26

Every Christmas we draw names for gift exchanges in my family. This usually involves mulitple redraws until no one has pulled their spouse. So this year I coded up my own

9条回答
  •  醉话见心
    2020-12-04 22:37

    I wouldn't use disallowed pairings, since that greatly increases the complexity of the problem. Just enter everyone's name and address into a list. Create a copy of the list and keep shuffling it until the addresses in each position of the two lists don't match. This will ensure that no one gets themselves, or their spouse.

    As a bonus, if you want to do this secret-ballot-style, print envelopes from the first list and names from the second list. Don't peek while stuffing the envelopes. (Or you could just automate emailing everyone thier pick.)

    There are even more solutions to this problem on this thread.

提交回复
热议问题