3 ms·
After reading “There’s no elegant solution to FizzBuzz”, immediately had to check how I might try to be unnecessarily clever if I had to do it in Python and arr
by strogonoff 6y ago
After reading “There’s no elegant solution to FizzBuzz”, immediately had to check how I might try to be unnecessarily clever if I had to do it in Python and arrived at:
for i in range(1, 100):
divisible_by_3 = i % 3 == 0
divisible_by_5 = i % 5 == 0
fizz = 'Fizz' if divisible_by_3 else ''
buzz = 'Buzz' if divisible_by_5 else ''
num = '' if divisible_by_3 or divisible_by_5 else i
print "{0}{1}{2}".format(fizz, buzz, num)
(In reality, of course, I would likely have had to look up the modulo operator first.)
- Zarel 6y agoJavaScript can eliminate all repetition thanks to how its || operator works: for (let i = 1; i <= 100; i++) { console.log( (i % 3 == 0 ? "Fizz" : "") + (i % 5 == 0 ? "Buzz" : "") || i ); } Whether or not you consider this more elegant is a matter of taste, though.
- strogonoff 6y agoWhile I like to think the opposite, I can see myself using || with an empty string occasionally (at least with TS, where I can be reasonably sure the variable will definitely be a string at runtime). However, concatenating strings with + is a bit implicit to my taste.
- yarcob 6y agoMaybe the fact that there is no elegant solution makes it useful.
- xoudini 6y agoHere's a (perhaps "unnecessarily clever") Python 3 version I wrote a while back, for some reason: import functools def fizzbuzz(n): if not n % 3: yield (r := "fizz") if not n % 5: yield (r := "buzz") if not "r" in locals(): yield n compose = lambda f, g: lambda *args: f(g(*args)) functions = "".join, functools.partial(map, str), fizzbuzz transform = functools.reduce(compose, functions) print("\n".join(map(transform, range(1, 100))))
- inoursweetz 6y agoThere is a nice paper, which explores possible elegant solutions to FizzBuzz. https://themonadreader.files.wordpress.com/2014/04/fizzbuzz.pdf https://themonadreader.files.wordpress.com/2014/04/fizzbuzz....
- strogonoff 6y ago> FizzBuzz in Haskell by Embedding a Domain-Specific Language I was not disappointed. > Meanwhile, we want there to be at most one place that outputs fizz, buzz, or the string representation, and each test to be performed only once. These do intuitively seem like the properties of an elegant solution.
- fanf2 6y agoEuler’s fizzbuzz is elegant http://philcrissman.net/posts/eulers-fizzbuzz/ http://philcrissman.net/posts/eulers-fizzbuzz/
- finnh 6y agoI've always thought that an elegant solution wouldn't iterate by ones, but would instead increment the correct amount between each print statement... 0+3+2+1+3+1+2+3+3+2+...
- makerofthings 6y agoI do it like this. fizzbuzz n = case (n^4 `mod` 15) of 1 -> show n 6 -> "fizz" 10 -> "buzz" 0 -> "fizzbuzz" main :: IO () main = print $ map fizzbuzz [1..30]
- eurasiantiger 6y agoThat is Euler’s FizzBuzz: http://philcrissman.net/posts/eulers-fizzbuzz/ http://philcrissman.net/posts/eulers-fizzbuzz/ I think it is an elegant solution.
- tandav 6y agoMy 2 cents: import itertools # this block is just for simple functional pipes class B: def __init__(self, f): self.f = f class Pipe (B): __ror__ = lambda self, x: self.f(x) class Map (B): __ror__ = lambda self, x: map (self.f, x) class MapValues(B): __ror__ = lambda self, it: it | Map(lambda kv: (kv[0], self.f(kv[1]))) # here the actual FizzBuzz ( range(1, 100) | Map(lambda i: (i, itertools.compress(('Fizz', 'Buzz'), (i % 3 == 0, i % 5 == 0)))) | MapValues(''.join) | Map(lambda kv: kv[1] or kv[0]) | Pipe(list) ) [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, ... ] if you're interested in pipes like above - here my simple library for that (no install needed, just copy some lines) https://github.com/tandav/pipe#fizzbuzz https://github.com/tandav/pipe#fizzbuzz