r/shittyprogramming • u/frublox • Jul 04 '19
Cracking the FizzBuzz interview with Python
I decided to take a break from my research into "Hello World" to tackle a challenging problem plaguing entry-level software developers everywhere. It's not easy, but contrary to popular opinion FizzBuzz can indeed be solved with Python, as demonstrated below.
class D:
def __init__(self, v): self.v = v
is_ = D((lambda _: lambda __: _(_)(__))
(lambda r: lambda l: (lambda h: lambda t: lambda _: h if _ else t)
(l[0])(r(r)(l[1:])) if l else None)
(list(range(1, 101))))
D.__bool__ = lambda _: is_.v(True) is not None if is_.v is not None else False
D.pop = lambda _: int((str(is_.v(True)) + str(exec("is_.v = is_.v(False)")))[:-4])
i = None
cs = ["i % 3 == 0 and i % 5 == 0", "i % 3 == 0", "i % 5 == 0"]; cp = 0
f = lambda _: lambda __: print(_)
w = lambda _: lambda __: print(__)
es = [f.__call__("FizzBuzz"), f.__call__("Fizz"), f.__call__("Buzz"),
w.__call__("Wuzz")]; ep = 0
ss = []; sp = 0
for _ in ".....!)~>!)~>!)~)- *(- ]*((- ]]*(((- ]]]*((((-":
exec('v={".": "ss.append(\'\')", " ": "sp += 1"}.get(_, ""); exec(v)')
if not v: ss[sp] += _
sp = 0
psp = 0; si = 0; r = 0
while not (not i and not is_):
if not i: i = is_.pop()
exec({"!": "r = eval(cs[cp])", ")": "psp += 1", "(": "psp -= 1",
"~": "if r: sp = psp; si = -1; cp = 0; ep = 0",
">": "cp += 1", "-": "sp = psp; si = -1; cp = 0; ep = 0",
"]": "ep += 1", "*": "es[ep](i); i = None"}
.get(ss[sp][si]))
si += 1As usual, a demo can be found here: https://ideone.com/Ncstp7.
I hope this helps you with your next coding interview!
147
Upvotes
59
u/Flaming_Eagle Jul 04 '19
Finally someone with an eloquent solution to this problem.
21
u/timawesomeness Jul 04 '19
Help I ran this and it summoned a demon
18
u/Rangsk Jul 04 '19
Isn't it nice how python basically reads like pseudocode?
15
u/skunkwaffle Jul 04 '19
On mobile the formatting is exquisite.
10
u/thebpfeif Jul 04 '19
I think you missed a lambda somewhere in there.
6
u/Hypersapien Jul 04 '19
This shit right here is why I do C#
6
u/hilomania Jul 05 '19
I know this is a joke, but assuming someone seriously came up with this in a job interview: I would totally hire this person. It shows an absolute understanding of Python's breadth. I would hand that person only the problems my team had no good solution for to begin with. Basically make it our "hail Mary programmer".
3
u/Luapix Jul 04 '19
Good god, OP, you really outdid yourself on this one. Maybe I'll try to untangle it when I have the time xD
3
u/wakeupsheep Jul 04 '19
AMA request job applicant who did a /r/shittyprogramming/ or /r/programminghorror worthy, but working as intended, FizzBuzz.
2
u/HashtagFour20 Jul 05 '19
thanks i want to shoot myself now
1
u/c_o_r_b_a Jul 11 '19
I love reading code written by people who don't know De Morgan's laws. I'm surprised how often I see conditionals similar to this in real code.
1
u/Dads101 Aug 27 '19
I literally am dying
0
u/[deleted] Jul 04 '19
[deleted] — view deleted comment
0
u/[deleted] Jul 04 '19
[deleted] — view deleted comment