我决定做一个Python程序,根据硬编码的限制生成秘密的圣诞老人对(例如,某人找不到他的妻子)。我的家庭成员都很忙,所以很难组织每个人随意画帽子。在
我的程序死机很少,因为不幸的随机配对使剩余的一对非法(不过,我在测试脚本部分捕捉到它们)。你可以把它想象成一个亲自重新画的画。在
然而,当我的程序成功时,我知道配对是正确的,而不必亲自查看它们,因为我的程序会测试其有效性。明天,我要想办法利用配对,从我的邮箱给每个人发一封电子邮件,告诉他们他们有谁,而我不知道谁拥有谁,甚至谁拥有我。在
以下是我的完整程序代码(所有代码都是我自己的;我没有在网上查找任何解决方案,因为我想让自己接受最初的挑战):# Imports
import random
import copy
from random import shuffle
# Define Person Class
class Person:
def __init__(self, name, email):
self.name = name
self.email = email
self.isAllowedToHaveSecretSanta = True
self.isSecretSanta = False
self.secretSanta = None
# ----- CONFIGURATION SCRIPT -----
# Initialize people objects
brother1 = Person("Brother1", "brother1@gmail.com")
me = Person("Me", "me@gmail.com")
brother2 = Person("Brother2", "brother2@gmail.com")
brother2Girlfriend = Person("Brother2Girlfriend", "brother2Girlfriend@gmail.com")
brother3 = Person("Brother3", "brother3@gmail.com")
brother3Girlfriend = Person("Brother3Girlfriend", "brother3Girlfriend@gmail.com")
brother4 = Person("Brother4", "brother4@gmail.com")
brother4Girlfriend = Person("Brother4Girlfriend", "brother4Girlfriend@gmail.com")
brother5 = Person("Brother5", "brother5@yahoo.com")
brother5Girlfriend = Person("Brother5Girlfriend", "brother5Girlfriend@gmail.com")
myDad = Person("MyDad", "myDad@gmail.com