my $i;
my $fizz = 3;
my $buzz = 5;
for ($i = 1; $i <= 100; $i++) {
if ($i ne $fizz && $i ne $buzz) {
print "$i\n";
next;
}
if ($i eq $fizz) {
print "fizz";
$fizz += 3;
}
if ($i eq $buzz) {
print "buzz";
$buzz += 5;
}
print "\n";
}
Edit for formattingAnd because I had to fiddle around manually to understand it (not good at Ruby's inbuilt methods) here's how it works: It generates two long arrays, one containing "Fizz" every third element, and one containing "Buzz" every five elements, the rest is filled up with nils.
Then it iterates over 1 to 100 and zips the two arrays at that position together - if the "zipped together" sum of both arrays is nil, that number is printed, else the concatenated string of both arrays at that position is printed (example: "Fizz" + nil = "Fizz"). Neat!
Edit: I also found a great one using flip-flops: https://gist.github.com/3230984
That said, I guess I should rephrase my question: Is there a simple programming test that can help weed out fakers quickly, which does not involve mathematics? Sort of like "Deploy a simple CRUD app in 15 mins" or something?
This is to address the parent's concern that a lot of web apps out there don't actually require much mathematics.
''.join(filter(None, [a, b]))
and
itertools.cycle([None, None, 'Fizz'])
itertools.cycle([None, None, None, None, 'Buzz'])
As an example i'm working on a largish codebase (100,000 lines) right now now and a quick grep shows only 4 uses of it, all of which are in a jQuery plugin.