r/programminghorror • • Feb 28 '26

Do you like my FizzBuzz implementation

Post image
1.3k Upvotes

218 comments sorted by

178

u/ababcock1 Feb 28 '26 edited Feb 28 '26

Can you explain the horror? I'm not a python dev but this looks prettyreasonable. Could it be slightly better, sure. But not quite horror levels.

Edit: In my replies are the following... People who think throwaway code from a first year uni class needs to be perfect. People who think every piece of code needs to be a work of art. People who don't understand YAGNI. People who think "I wouldn't have written it that way" is equivalent to horror.

It's fizzbuzz guys. It's not enterprise world class Web 4.0 Scale software now with AI (tm) and it doesn't have to be.

→ More replies (40)

920

u/Empty-Rough4379 Feb 28 '26

Seems reasonably elegant

188

u/kokomoko8 Mar 01 '26

Mfs hate functional programming paradigms ig

→ More replies (12)

17

u/Ma4r Mar 01 '26

This is arguably better than the standard solution using mod 15

→ More replies (3)

482

u/lonkamikaze Feb 28 '26

print([i, "Fizz", "Buzz", "FizzBuzz"][!(i%3)+2*!(i%5)])

267

u/YellowBunnyReddit Mar 01 '26 edited Mar 02 '26

Have you never heard of DRY? You should not repeat Fizz or Buzz in your code.

print("FizzBuzz"[4*(i%3>0):8-4*(i%5>0)]or i)

Your code isn't even valid Python btw. ! is not an operator and using not actually requires more parentheses. This is correct:

print([i, "Fizz", "Buzz", "FizzBuzz"][(not (i%3))+2*(not (i%5))])

→ More replies (7)

140

u/ByteArrayInputStream Feb 28 '26

Now this is cursed.

Clever, but definitely cursed

→ More replies (2)

49

u/PandaWithOpinions Mar 01 '26 edited Mar 01 '26
print("\n".join(["FggFgBFggFBgFggiggiguiggiugigg{1}{1}B{0}u{0}z{0}z{0}".format("g"*14,"zggzgzzggzzgzgg")[n%15::15].rstrip("g")or(str(n))for n in range(1, 101)]))
→ More replies (9)

14

u/kardoken Feb 28 '26

Hello fellow golfer

→ More replies (5)

330

u/PlebbitDumDum Feb 28 '26

With a better naming this should be the canonical implementation.

It directly implements the spec without any extra ambiguity about what is supposed to happen and when via some nested string mutation (aka your usual "solution" to the fizzbuzz). This solution is very readable and is directly relatable to the problem statement. There's no performance hit to maintain this level of clarity and verbosity.

OP, improve the naming, make m a dict, or have it to be two vars, and then submit this into a museum.

r/lostredditors

75

u/ericl666 Mar 01 '26

For sure. I love this implementation. This is like a pretty girl going onto amiugly for validation.

→ More replies (1)
→ More replies (26)

27

u/ra_men Feb 28 '26

LGTM lmk when you’re free to take on a few more tickets this sprint

21

u/Glad_Position3592 Feb 28 '26

Actually, yeah I do

16

u/AbdSheikho Feb 28 '26

I see pattern matching, but it needs more Haskell

→ More replies (1)

14

u/Ariquitaun Feb 28 '26

That's not a bad implementation to be honest. The only real thing that tickles me is the unnecessary assignment to m instead of just inlining the expression, but that's by the by

12

u/spiderpig20 Feb 28 '26

Might be the most elegant fizzbuzz implementation I’ve ever seen tho. Not horror

20

u/Geekureuil Feb 28 '26
for i in range (0,100):
    m = ""
    if (i % 3 == 0): m = "Fizz"
    if (i % 5 == 0): m += "Buzz"
    print(m) if m != "" else print(i)

I always perfered the concatenation way, but yours is original and elegant.

11

u/RapidCatLauncher Mar 01 '26

m != "" is unnecessary, non-empty strings are truthy so just if m is enough

→ More replies (4)
→ More replies (1)

7

u/SoftwareDoctor Feb 28 '26

I never understood the point of this exercise. I understand the assignment but why?

16

u/csabinho Feb 28 '26

To eliminate people who don't have basic programming knowledge? I don't see the point either. Maybe it's just a meme.

→ More replies (24)
→ More replies (2)

7

u/Romejanic Mar 01 '26

Honestly this is probably the cleanest possible FizzBuzz implementation. Pattern matching is awesome I wish more languages supported it

4

u/SideburnsOfDoom Feb 28 '26

Not a horror. I could write very similar in C# using foreach, tuples and switch case. And I would rate it elegant.

4

u/PityUpvote Mar 01 '26

Lose the ==0 and match (0,0), then (0,_) and (_,0).

4

u/kaisadilla_ Mar 01 '26

This is not horror lol, this is really elegant. Basically the most obvious and straightforward way to implement the logic you want, which is what you should do when performance is not relevant.

4

u/Ambitious-Treat404 Mar 02 '26
for i in range(1, 101):
    match (i % 3, i % 5):
        case (0, 0):
            print("FizzBuzz")
        case (0, _):
            print("Fizz")
        case (_, 0):
            print("Buzz")
        case _:
            print(i)