I stumbled upon this kata; the aim is to count how many epoqs it takes for n forward-counters and m backward-counters to all be equal.
In the beginning, all the planets start with 0.
There are two types of planets: forward-counting planets, and backward-counting planets. Every second, the display number on each forward-counting planet will increase by one, and the display number for each backward-counting planet will decrease by one.
Each planet has it's own special number, let's call it the planet's x, for now. It is the highest number that the planet can display. The lowest is 0.
Instead of becoming negative, the display number for a backward-counting planet would wrap to it's x.
Instead of becoming greater than x, the display number for a forward-counting planet would wrap back to 0.
This code works and seem 'clean' but times out when the amount of counters gets too large. How can I optimize this code?
class Counter: def __init__(self, name): self.name = name self.modulo = name + 1 class ForwardCounter(Counter): def __init__(self, name): super().__init__(name) def count(self, epoq): return (self.name + epoq) % self.modulo class BackwardCounter(Counter): def __init__(self, name): super().__init__(name) def count(self, epoq): return (self.name - epoq) % self.modulo def unifier(retrograde,prograde): counters = [BackwardCounter(x) for x in retrograde] counters += [ForwardCounter(x) for x in prograde] epoq = 1 while True: first = counter.count(epoq) if all(first == rest.count(epoq) for rest in counter[1:]): break epoq += 1 return epoq
test.assert_equals(unifier([25,8], [24,7]), 1402) test.assert_equals(unifier([1,2,3,4], [6,7,8,9,10]), 27720)