From 29bcc1f8824ff90a77152a5fffb6d9e11b4f520c Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Mon, 6 Jul 2015 17:17:39 -0700 Subject: [PATCH] hats problem --- dotfiles/lib/python/hats_problem.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 dotfiles/lib/python/hats_problem.py diff --git a/dotfiles/lib/python/hats_problem.py b/dotfiles/lib/python/hats_problem.py new file mode 100644 index 00000000..5725b625 --- /dev/null +++ b/dotfiles/lib/python/hats_problem.py @@ -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)