Skip to content

Instantly share code, notes, and snippets.

@ruckuus
Created January 30, 2014 11:52
Show Gist options
  • Save ruckuus/8706980 to your computer and use it in GitHub Desktop.
Save ruckuus/8706980 to your computer and use it in GitHub Desktop.
FizzBuzz
<?php
class Numlet{
protected $fizz = false;
protected $buzz = false;
public function isFizz($n) {
if ($n % 3 == 0) {
$this->fizz = true;
return true;
}
}
public function isBuzz($n) {
if ($n % 5 == 0) {
$this->buzz = true;
return true;
}
}
public function isNormal($n) {
if (!$this->fizz && !$this->buzz) {
return false;
}
}
public function fizzBuzz($n) {
if ($this->isFizz($n)) {
echo "Fizz ";
}
if ($this->isBuzz($n)) {
echo "Buzz ";
}
if ((!$this->fizz) && (!$this->buzz)) {
echo $n . " ";
}
}
}
for ($i = 1; $i < 20; $i++) {
$x = new Numlet();
$x->fizzBuzz($i);
}
<?php
$out = array_map(function($n) { return (''==($m=(($n % 3 == 0) ? "Fizz" : '') . ( ($n % 5 == 0) ? "Buzz" : '')) )?$n:$m; }, range(1,100));
foreach($out as $i) {
echo "${i}\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment