Re:Subverting the Software Interview
by ptspts on Sunday January 15, 2023 @07:44PM
(#34395364)
There is “no way” to subvert this FizzBuzz interview when writing code in Python 3 only.
For bonus points: which alphanumeric characters can be changed in the code above?
[ Parent
| on HN ]
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))Here is another version of similar idea. (no loops and no conditions)
fizz_buzz = lambda n : (n == 0) or not fizz_buzz(n-1) or not print(['{}',"fizz","buzz","fizzbuzz"][((n%3)==0)|(((n%5)==0)*2)].format(n))
fizz_buzz(100)
Re:Subverting the Software Interview
by rahimnathwani on Monday January 16, 2023 @11:40AM
(#34402417)
Nice use of bitwise OR
Re:Subverting the Software Interview
by sjadoinqwoeihad on Monday January 16, 2023 @04:38AM
(#34398716)
> 0, 0, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 0, 0
I never noticed this symmetry in fizzbuzz before.
It seems like I'm missing a joke.
Is this style in homage to the linked-to page? I can't figure out why you're not using itertools nor re-using itertools names. Is it that translating from .. is that Haskell? .. to Python without idiomatic translation the subversion?
from itertools import islice, cycle
def fizzbuzz():
return (a + b or str(c) for c, (a, b) in enumerate(zip(
cycle(("", "", "Fizz")),
cycle(("", "", "", "", "Buzz")),
), start = 1))
>>> print(*islice(fizzbuzz(), 0, 30))
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz