4 ms·
There are much simpler ways without using modulo than defining your own modulo and using it. For example (in Java) class FizzBuzz { public static void
by bugstomper 11y ago
There are much simpler ways without using modulo than defining your own modulo and using it. For example (in Java)
class FizzBuzz {
public static void main(String[] argc) {
int next5 = 5;
int next3 = 3;
for (int i=1; i<=100 ; i++ ) {
String output = "";
if (i == next3) {
output = "Fizz";
next3 += 3;
}
if (i == next5) {
output += "Buzz";
next5 += 5;
}
if (output.isEmpty()) {
output = Integer.toString(i);
}
System.out.println(output);
}
return;
}
}
- dalke 11y agoYes, that works. But I don't understand the goal of these variant question. The original goal was to weed out obviously incompetent programmers. I accept that for some positions that's reasonable. But if I were given that question, and end up implementing my own modulo function to replace the missing operator, is that a mark against the interviewee, compared to someone with otherwise identical qualifications who implements your solution? Or someone who does this sieve solution: def fizzbuzz(n): n += 1 values = ["%d"]*(n+1) for i in range(0, n, 3): values[i] = "%d Fizz" for i in range(0, n, 5): values[i] = "%d Buzz" for i in range(0, n, 15): values[i] = "%d FizzBuzz" for i in range(1, n): print values[i]%i All of these have different performance/memory/maintenance characteristics, but the full constraints are unspecified. At some point it feels like the interviewer just wants to see the monkey dance, by throwing in non-realistic constraints.