Created
October 7, 2012 21:59
-
-
Save davidwparker/3849751 to your computer and use it in GitHub Desktop.
Christmas randomization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import random | |
class Simulation: | |
def run(self): | |
self.names = [{'name':'david','spouse':'tyler'},{'name':'tyler','spouse':'david'}, | |
{'name':'jon','spouse':'bonnie'},{'name':'bonnie','spouse':'jon'}, | |
{'name':'jake','spouse':'caitlin'},{'name':'caitlin','spouse':'jake'}, | |
{'name':'dad', 'spouse':'mom'},{'name':'mom','spouse':'dad'}] | |
okay = False | |
added = [] | |
assignments = [] | |
# Continuously attempt to pair until okay | |
while not okay: | |
# For every single name in the list grab a random one and check against rules | |
for name in list(self.names): | |
# Rules | |
# 1) Can't draw self or spouse | |
# 2) Can't add someone twice | |
for_name = name | |
while (name['name'] == for_name['name'] | |
or name['spouse'] == for_name['name'] | |
or for_name in added): | |
random.seed() | |
for_name = random.choice(self.names) | |
# It's okay, add to assignments list and added list | |
added.append(for_name) | |
assignments.append(' '.join([name['name'],'buys for',for_name['name']])) | |
# if we get to mom, we're okay! | |
if name['name'] == 'mom': | |
okay = True | |
print '' | |
for s in assignments: | |
print s | |
if __name__ == '__main__': | |
simulation = Simulation() | |
cProfile.run('simulation.run()') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import random | |
class Simulation2: | |
def run(self): | |
self.names = [{'name':'david','spouse':'tyler'},{'name':'tyler','spouse':'david'}, | |
{'name':'jon','spouse':'bonnie'},{'name':'bonnie','spouse':'jon'}, | |
{'name':'jake','spouse':'caitlin'},{'name':'caitlin','spouse':'jake'}, | |
{'name':'dad', 'spouse':'mom'},{'name':'mom','spouse':'dad'}] | |
self.newnames = list(self.names) | |
okay = False | |
assignments = [] | |
# Continuously attempt to pair until okay | |
while not okay: | |
random.shuffle(self.newnames) | |
dictionary = zip(self.names, self.newnames) | |
# For every single name in the list grab a random one and check against rules | |
for di in dictionary: | |
# Rules | |
# 1) Can't draw self or spouse | |
# 2) Can't add someone twice | |
if (di[0]['name'] == di[1]['name'] | |
or di[0]['spouse'] == di[1]['name']): | |
assignments = [] | |
break | |
else: | |
# It's okay, add to assignments list and added list | |
assignments.append(' '.join([di[0]['name'],'buys for',di[1]['name']])) | |
# if we get to 8, we're okay! | |
if len(assignments) == 8: | |
okay = True | |
print '' | |
for s in assignments: | |
print s | |
if __name__ == '__main__': | |
simulation2 = Simulation2() | |
cProfile.run('simulation2.run()') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment