Last active
May 23, 2022 23:55
-
-
Save Gorcenski/f03c834696cb94768561283cfb9b82b2 to your computer and use it in GitHub Desktop.
The most obnoxious solution to FizzBuzz I can imagine.
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 numpy as np | |
from functools import reduce | |
class Buzzer: | |
def __init__(self, **kwargs): | |
values = [v for k, v in kwargs.items()] | |
self.kwargs = kwargs | |
self.lcm = np.lcm.reduce(values) | |
self.eps = 1e-7 | |
self.p = self.polybuilder(reduce(lambda q, r : np.convolve(q, r), | |
[self.polyvec(self.lcm // v) for v in values], | |
[1]), self.lcm) | |
@staticmethod | |
def polyvec(order): | |
return np.concatenate(([1], np.zeros(order - 1), [-1])) | |
@staticmethod | |
def polybuilder(f, lcm): | |
return lambda x : sum(f * np.array([np.exp(1j * x * k * 2 * np.pi / lcm) | |
for k in range(len(f) - 1, -1, -1)])) | |
def fizzbuzz(self, i): | |
return reduce(lambda x, y : x + y, | |
[k * bool(np.abs(self.polybuilder(self.polyvec(self.lcm // v), self.lcm)(i)) < self.eps) | |
for k, v in self.kwargs.items()], | |
str(i) * bool(np.abs(self.p(i)) > self.eps)) | |
print([Buzzer(fizz=3, buzz=5, baz=7, bar=11).fizzbuzz(i) for i in range(100)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment