Re:People suck at technical interviews (2014)
by squeaky-clean on Friday June 10, 2016 @12:39PM
(#11877647)
There's a lot of ways you can do it. I think it's fun to see how people solve it when they're not allowed to use the obvious answers (note: I don't interview people and probably would not choose no-modulo-fizzbuzz as an interview question).
Similar to your solution, instead of two counters, you can have two iterators that cycle every 3 / 5 elements, and add them. Then fill in any blank elements with their index+1.
from itertools import cycle, zip_longest, islice
fizz_list = ['', '', 'fizz']
buzz_list = ['', '', '', '', 'buzz']
output = [
fizz + buzz for fizz, buzz
in islice(zip_longest(cycle(fizz_list), cycle(buzz_list)), 0, 100)
]
for i in range(len(output)):
print(output[i] or i+1)
Similar to your solution, instead of two counters, you can have two iterators that cycle every 3 / 5 elements, and add them. Then fill in any blank elements with their index+1.