3 ms·
I always liked the version with no branching statements. In python it would be roughly: def fizz_str(n): return "FIZZ" def buzz_str(n):
by Kamq 4y ago
I always liked the version with no branching statements. In python it would be roughly:
def fizz_str(n):
return "FIZZ"
def buzz_str(n):
return "BUZZ"
def fizz_buzz_str(n):
return "FIZZBUZZ"
def to_str(n):
return str(n)
indexes = [3, 0, 0, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 0, 0]
def fizz_buzz(n):
funcs = [
to_str,
fizz_str,
buzz_str,
fizz_buzz_str
]
return funcs[indexes[n % 15]](n)
for i in range(1, 101):
print(fizz_buzz(i))
- raluk 4y agoHere is another version of similar idea as oneliner. Additional feature - no loops and no conditions. fb = lambda n : n == 0 or not fb(n-1) or not print(['{}',"fizz","buzz","fizzbuzz"][((n%3)==0)|(((n%5)==0)*2)].format(n)) fb(100)
- rahimnathwani 4y agoNice use of bitwise OR
- sjadoinqwoeihad 4y ago> 0, 0, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 0, 0 I never noticed this symmetry in fizzbuzz before.