Created
May 7, 2015 13:04
-
-
Save JosephMaxwell/43c0ddb46f4eb11fa0d1 to your computer and use it in GitHub Desktop.
FizzBuzz kata without control statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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