コード例 #1
0
def allocate_santas(secret_santa):
    timeout_total = time.time() + 20
    while True:
        if time.time() > timeout_total:
            return

        gift_pool = []
        for r in secret_santa.rounds:
            for santa in secret_santa.santas:
                gift_pool.append(Gift(recipient=santa, budget=r.budget))

        for santa in secret_santa.santas:
            budget_list = [r.budget for r in secret_santa.rounds]

            timeout = time.time() + 3  # allowed seconds in the loop
            while len(santa.gifts) != secret_santa.number_rounds():
                if time.time() > timeout:
                    break
                if secret_santa.unique_recipients:
                    possible_gifts = [
                        g for g in gift_pool
                        if g.recipient != santa and g.budget in budget_list
                        and not santa.is_recipient(g.recipient)
                    ]
                else:
                    possible_gifts = [
                        g for g in gift_pool
                        if g.recipient != santa and g.budget in budget_list
                    ]
                if possible_gifts:
                    random_gift = random.choice(possible_gifts)
                    santa.add_gift(random_gift)
                    budget_list.remove(random_gift.budget)
                    gift_pool.remove(random_gift)

        ok = True
        for santa in secret_santa.santas:
            if not len(santa.gifts) == secret_santa.number_rounds():
                ok = False
                break
        if ok:
            return secret_santa
コード例 #2
0
with open('girllist.csv', 'r') as f:
    reader = csv.reader(f)
    girllist = list(reader)

with open('giftlist.csv', 'r') as f:
    reader = csv.reader(f)
    giftlist = list(reader)

BoyObjectList = []
GirlObjectList = []
CoupleObjectList = []
GiftObjectList = []

for i in giftlist:
	GiftObjectList.append(Gift(i[0],i[1],i[2],i[3]))

GiftObjectList = sorted(GiftObjectList,key=lambda x: x.cost)

for i in boylist:
	BoyObjectList.append(Boy(i[0],i[1],i[2],i[3],i[4],i[5]))

for i in girllist:
	GirlObjectList.append(Girl(i[0],i[1],i[2],i[3],i[4]))

for gl in GirlObjectList:
	for by in BoyObjectList:
		logs(gl.name+" is looking guy "+by.name)
		if by.rstatus == gl.rstatus and by.rstatus== "single" and by.is_eligible(gl.maintenance_budget,gl.attractiveness) and gl.is_eligible(by.budget):
			by.set_girlfriend = gl.name
			by.rstatus = "committed"