function solution($startIndex, $endIndex, $map, $glue = " ") { $fizzBuzzed = []; foreach (range($startIndex, $endIndex) as $index) { $fizzBuzzed[] = fizzBuzz($index, $map); } $output = implode($glue, $fizzBuzzed); return $output . "\n"; }
/** * FizzBuzz w/ Recursion * Jamie Doris */ function fizzBuzz($num) { if ($num < 101) { $nextnum = $num + 1; if ($num % 3 == 0 && $num % 5 == 0) { // divisible by both 3 and 5 return "FizzBuzz\n" . fizzBuzz($nextnum); } if ($num % 3 == 0) { // divisible by 3 return "Fizz\n" . fizzBuzz($nextnum); } if ($num % 5 == 0) { // divisible by 5 return "Buzz\n" . fizzBuzz($nextnum); } return $num . "\n" . fizzBuzz($nextnum); // not divisible by 3 or 5, so just print the number } return false; }
/** * Traditional FizzBuzz * * Question: Print out the numbers 1 to n with the following caveats: * * - If the number is divisible by 3, print Fizz * - If the number is divislble by 5, print Buzz * - If the number is divisible by 3 and 5, print FizzBuzz * * Input: Max number to print to, n * * Output: Printed list of numbers */ function fizzBuzz($n) { for ($i = 1; $i <= $n; $i++) { $print = ''; if ($i % 3 == 0) { $print .= 'Fizz'; } if ($i % 5 == 0) { $print .= 'Buzz'; } if (empty($print)) { $print = $i; } echo "{$print}\r\n"; } } fizzBuzz(100);
public function testNumber13() { $this->assertSame(13, fizzBuzz(13)); }
public function testFizzBuzzNullは空文字列で返す() { $this->assertIdentical('', fizzBuzz()); }