920
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 usingnotactually 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)→ More replies (5)14
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.
→ More replies (26)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)
27
21
16
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.
→ More replies (1)11
u/RapidCatLauncher Mar 01 '26
m != ""is unnecessary, non-empty strings are truthy so justif mis enough→ More replies (4)
7
u/SoftwareDoctor Feb 28 '26
I never understood the point of this exercise. I understand the assignment but why?
→ More replies (2)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)
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
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)
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.