hats problem

This commit is contained in:
Ivan Malison 2015-07-06 17:17:39 -07:00
parent 103d6f37ef
commit 29bcc1f882

View File

@ -0,0 +1,27 @@
import random
class HatsProblem(object):
def __init__(self, size):
self.size = size
def build_hats(self):
return [self.hat() for _ in range(self.size)]
def hat(self):
return random.randint(0, self.size - 1)
def go(self):
hats = self.build_hats()
guesses = [
self.calculate_guess_modulus(self.sum_of_all_but_i(i, hats), i)
for i in range(self.size)
]
return zip(hats, guesses)
def calculate_guess_modulus(self, current, desired):
return ((desired - current) + self.size) % self.size
def sum_of_all_but_i(self, i, hats):
return sum(hat for index, hat in enumerate(hats) if index != i)