by softbuilder · 12y ago · 0 comments
[(not x % 3) * 'Fizz' + (not x % 5) * 'Buzz' or x for x in range(1, 101)]Of course, this is true to a certain extent of all programming languages, but I do find Python particularly easy in this respect.
['FizzBuzz' if 0 == n % 15 else 'Fizz' if 0 == n % 3
else 'Buzz' if 0 == n % 5 else str(n) for n in range(1,101)]If you make the generator always return a string, then you can make it print, thus:
print '\n'.join((not x % 3) * 'Fizz' + (not x % 5) * 'Buzz' or str(x) for x in range(1, 101)) True * "String"
>"String"
And... not 0
>True
I guess knowing the "truthiness" of all regular Python types is useful.or
let f = (replicate . fromEnum . (==0)) .: mod in [concat $ f x 3 "Fizz" ++ f x 5 "Buzz" | x <- [1..101]]