Skip to content

Instantly share code, notes, and snippets.

@JosephMaxwell
Created May 7, 2015 13:04
Show Gist options
  • Save JosephMaxwell/43c0ddb46f4eb11fa0d1 to your computer and use it in GitHub Desktop.
Save JosephMaxwell/43c0ddb46f4eb11fa0d1 to your computer and use it in GitHub Desktop.
FizzBuzz kata without control statements
<?php
function getMap($input, $function) {
$numbers = getNumbers();
$array = array_filter($numbers, $function);
return array_combine($array, array_fill(0, count($array), $input));
}
function getNumbers()
{
$numbers = range(0,100);
unset($numbers[0]);
return $numbers;
}
$numbers = getNumbers();
$neither = array_filter($numbers, function($value) {
return ($value % 3) !== 0 && ($value % 5) !== 0;
});
$fizz = getMap("fizz", function($value) {
return ($value % 3) === 0 && ($value % 5) !== 0;
});
$buzz = getMap("buzz", function($value) {
return ($value % 3) !== 0 && ($value % 5) === 0;
});
$fizzbuzz = getMap("fizzbuzz", function($value) {
return ($value % 3) === 0 && ($value % 5) === 0;
});
$output = $neither + $fizz + $buzz + $fizzbuzz;
ksort($output);
print_r($output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment