def fizzbuzz_no_modulo():
counter_three = 1
counter_five = 1
for i in range(1, 101):
output = []
if counter_three == 3:
output.append('Fizz')
counter_three = 0
if counter_five == 5:
output.append('Buzz')
counter_five = 0
if not output:
output.append(str(i))
print(''.join(output))
counter_three += 1
counter_five += 1Similar 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)"Okay, let's assume I have some function that tells me whether something is divisible by p. [writes it while using that function] Now, how would I implement that function? Well, I'd search for some existing library function to do that, but if I can't, then here's how I would check divisibility ..."
That's because the senior dev knows how to break the problem into solvable pieces, even and especially where they don't already know the solution to each piece.
I've seen numerous people fail on FizzBuzz despite supposedly being senior engineers.
None of them failed because they didn't know the modulo operator. For one thing, it's actually pretty easy to solve even without modulo.
If someone doesn't know modulo, I'll happily explain it.
More commonly, people struggle with even basic things like setting up a loop at all.
If you don't do fizzbuzz or equivalent, how else do you catch that?
It's surprisingly easy actually.
var remainder = x - Math.floor(x/y)*y
(Math.floor because javascript)
> Wait, what? No, it asks "can you write a for loop without breaking a sweat?".
There are notorious cases where senior engineers with years of actual honest coding experience "fail" fizzbuzz. IMO, That happens in two ways:
1) Not knowing (forgotten?) what a modulus operator is, and trying to code one up from scratch with a for loop. This can totally happen when you spend a few years working on line-of-business CRUD apps that never turn out to need modulus arithmetic.
Coding modulus from scratch with just + - / * is a fun little challenge in its own right, but it'll probably take you longer than 10 minutes to write it correctly and solve fizzbuzz with it.
That means you just took more than 10 minutes on fizzbuzz, which means you failed.
2) Trying to "clean up" the solution. http://c2.com/cgi/wiki?FizzBuzzTest has a good discussion on 'Why Fizz-Buzz is "hard:"', which points out that many of the most obvious solutions have code duplication, which some people then try to eliminate, tying themselves in knots trying to make it perfect.
The third possibility, that a candidate with years of experience shipping code nonetheless struggles to write a for loop, is IMO just not as common as the above two failure cases. (Much more likely is that the candidate has lied/exaggerated on their resume.)