[challenge] Extensible fizzbuzz 08-01-2017, 07:40 PM
#1
Believe it or not, the seemingly introductory FizzBuzz challenge is actually great for interview questions. Since many CS degrees are all theory and no actual code, it helps weed out those who don't know how to program. The standard FizzBuzz (explained below) covers more critical areas of programming than some might realize, but to take it one step further, let's make it extensible.
Before we get into the full challenge, let's cover the meat and potatoes of it. The standard FizzBuzz specification is as follows: print "Fizz" for multiples of three, "Buzz" for multiples of five, both for multiples of 15 (LCM of 3 and 5), and the number in any other case. To extend this, write a program that covers not only the above spec for the numbers 1 to 100, but has the capability of tacking on more multiples. For example, if we wanted to print "Beep" on multiples of four, the output would look something like this:
My solution:
Before we get into the full challenge, let's cover the meat and potatoes of it. The standard FizzBuzz specification is as follows: print "Fizz" for multiples of three, "Buzz" for multiples of five, both for multiples of 15 (LCM of 3 and 5), and the number in any other case. To extend this, write a program that covers not only the above spec for the numbers 1 to 100, but has the capability of tacking on more multiples. For example, if we wanted to print "Beep" on multiples of four, the output would look something like this:
Code:
1
2
Fizz
Beep
Buzz
Fizz
7
...
14
FizzBuzz
Beep
17
...
59
FizzBeepBuzzSpoiler: hint
Hashes/dictionaries/maps/associative arrays/whatever they're called in your language of choice are a big help here.
My solution:
Spoiler: Perl; normal
Any new multiple/word pairs are added to the %dvs hash.
The "map" block will return nothing, "()", if the current key from %dvs is not evenly divisible by $i, the counter (i.e., $i modulus the key is not zero). If it is divisible, it returns the value of that key in %dvs.
The join simply concatenates them all together, forming a string, and the "||$i" part at the end uses the $i scalar if the other part returned an empty string (i.e. no matches).
The "map" block will return nothing, "()", if the current key from %dvs is not evenly divisible by $i, the counter (i.e., $i modulus the key is not zero). If it is divisible, it returns the value of that key in %dvs.
The join simply concatenates them all together, forming a string, and the "||$i" part at the end uses the $i scalar if the other part returned an empty string (i.e. no matches).
Code:
use strict;
my %dvs=(3 => "Fizz",5 => "Buzz");
for my $i(1..100){
printf "%s\n",(join '',map {$i%$_?():$dvs{$_}} sort keys %dvs)||$i;
}Spoiler: Perl; golfed, 89 bytes
Follows the same logic as the above solution.
To cut out the extra bytes, I used array notation in constructing the hash, some sneaky use of hash selection, and removed lots of whitespace.
I also did not use the strict pragma in order to save bytes with the "my" declaration and the call to the pragma itself.
To cut out the extra bytes, I used array notation in constructing the hash, some sneaky use of hash selection, and removed lots of whitespace.
I also did not use the strict pragma in order to save bytes with the "my" declaration and the call to the pragma itself.
Code:
%d=qw(3 Fizz 5 Buzz);for$i(1..100){printf"%s\n",(join'',@d{grep$i%$_<1,sort keys%d})||$i}
(This post was last modified: 08-01-2017, 07:50 PM by Inori.)
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.
don't have much in the first place, who feel the new currents and ride them the farthest.
















![[+]](https://sinister.li/images/modern/collapse_collapsed.png)

