<?php function fizzbuzz($num) { if ($num % 15 == 0) { print "FizzBuzz" . PHP_EOL; } else { if ($num % 5 == 0) { print "Buzz" . PHP_EOL; } else { if ($num % 3 == 0) { print "Fizz" . PHP_EOL; } else { print $num . PHP_EOL; } } } } for ($i = 0; $i <= 100; $i++) { fizzbuzz($i); }
<?php function fizzbuzz($val, $num, $str) { return $val % $num == 0 ? $str : false; } foreach (range(1, 19) as $i) { $val = fizzbuzz($i, 15, 'FizzBuzz') ?: fizzbuzz($i, 5, 'Fizz') ?: fizzbuzz($i, 3, 'Buzz') ?: $i; echo $val . PHP_EOL; }
function fizzbuzz_caller($start, $end) { echo fizzbuzz($start) . "\n"; if ($start < $end) { fizzbuzz_caller($start + 1, $end); } }
*/ function fizzbuzz($data) { // initialise the return value as a string $strReturnValue = ""; // separate each values and the length of the value $arSeparateValues = explode(" ", $data); // initialise $intFizz = $arSeparateValues[0]; $intBuzz = $arSeparateValues[1]; $intLength = $arSeparateValues[2]; // loop through all strings for ($i = 1; $i <= $intLength; $i++) { if ($i % $intFizz == 0 && $i % $intBuzz == 0) { $strReturnValue .= "FB "; } elseif ($i % $intFizz == 0) { $strReturnValue .= "F "; } elseif ($i % $intBuzz == 0) { $strReturnValue .= "B "; } else { $strReturnValue .= "{$i} "; } } echo trim($strReturnValue); } $fh = fopen($argv[1], "r"); while (!feof($fh)) { $data = trim(fgets($fh)); fizzbuzz($data); echo "\n"; }
<?php function fizzbuzz($fizz, $buzz, $count) { $result = ""; for ($i = 1; $i <= $count; $i++) { if ($i % $fizz == 0 && $i % $buzz == 0) { $result .= "FB "; } elseif ($i % $fizz == 0) { $result .= "F "; } elseif ($i % $buzz == 0) { $result .= "B "; } else { $result .= "{$i} "; } } $result = trim($result); return $result; } $fh = fopen($argv[1], "r"); while (!feof($fh)) { $test = trim(fgets($fh)); if ($test != "") { $file_data = explode(" ", $test); $fizz = $file_data[0]; $buzz = $file_data[1]; $count = $file_data[2]; $result = fizzbuzz($fizz, $buzz, $count); echo $result . "\n"; } }
<?php function fizzbuzz($val, $num, $str) { return $val % $num == 0 ? $str : false; } function or_pl($a, $b) { return $a ?: $b; } foreach (range(1, 100) as $i) { echo or_pl(fizzbuzz($i, 15, 'FizzBuzz'), or_pl(fizzbuzz($i, 5, 'Fizz'), or_pl(fizzbuzz($i, 3, 'Buzz'), $i))) . PHP_EOL; }
<?php function fizzbuzz($val) { $conds = array(array('num' => 15, 'str' => 'FizzBuzz'), array('num' => 5, 'str' => 'Fizz'), array('num' => 3, 'str' => 'Buzz')); foreach ($conds as $cond) { if ($val % $cond['num'] == 0) { $val = $cond['str']; break; } } return $val; } foreach (range(1, 100) as $i) { echo fizzbuzz($i) . "\n"; }
} return $result; } $app = new \Slim\Slim(); $app->get('/fizzbuzz/:input+', function ($input) use($app) { // extract start and stop numbers from $input $range = explode(',', $input[0]); $result = fizzbuzz($range[0], $range[1]); $response = $app->response(); $response['Content-Type'] = 'application/json'; $response['X-Powered-By'] = 'PHP 5, Slim Framework, OpenShift'; echo json_encode(implode("\n", $result)); }); $app->get('/fizzbuzzarray/:input+', function ($input) use($app) { $range = explode(',', $input[0]); $result = fizzbuzz($range[0], $range[1]); $response = $app->response(); $response['Content-Type'] = 'application/json'; $response['X-Powered-By'] = 'PHP 5, Slim Framework, OpenShift'; echo json_encode($result); }); $app->get('/', function () { ?> <!doctype html><html><head><meta charset="utf-8"><title>FBaaS</title><body> <h1>FBaaS</h1> <p>Please review the specification for <a href="https://github.com/tomjakubowski/fbaas">FizzBuzz as a service</a>. </p> <p><a href="/fizzbuzz/1,100">Or click here and you can figure it out</a></p></body></html> <?php
<?php // https://www.codeeval.com/open_challenges/1/ function fizzbuzz($position, $fizz, $buzz) { if ($position % $fizz == 0 && $position % $buzz == 0) { return "FB"; } elseif ($position % $fizz == 0) { return "F"; } elseif ($position % $buzz == 0) { return "B"; } else { return $position; } } $fh = fopen($argv[1], "r"); while ($test = fgets($fh)) { if (empty($test)) { break; } $text = ""; list($fizz, $buzz, $count) = explode(" ", trim($test)); for ($i = 1; $i < $count; $i++) { $text .= fizzbuzz($i, $fizz, $buzz); $text .= " "; } $text .= fizzbuzz($count, $fizz, $buzz); echo $text . PHP_EOL; }