It takes 5 minutes for take1,2,3. so good.
- reference
-- http://www.aoky.net/articles/jeff_atwood/why_cant_programmers_program.htm
* subject
Let's programming 1 to 100 number print out.
but if number is multiple of three then print "Fizz",
,multiple of five then print "Buzz", and
multiple of three and file then print "FizzBuzz".
* take1
simple and stupidly honest.
<?php foreach (range(1, 100) as $n) { if ($n % 3 == 0) { if ($n % 5 == 0) { echo "FizzBuzz\n"; } else { echo "Fizz\n"; } } elseif ($n % 5 == 0) { echo "Buzz\n"; } else { echo "$n\n"; } }
* take2
aggregate %5 routine.
<?php foreach (range(1, 100) as $n) { $d = ''; if ($n % 3 == 0) { $d .= "Fizz"; } if ($n % 5 == 0) { $d .= "Buzz"; } if ($d === '') { $d = $n; } echo "$d\n"; }
* take2'
ternary operand
<?php foreach (range(1, 100) as $n) { $d = ($n % 3)?'':"Fizz"; $d .= ($n % 5)?'':"Buzz"; echo ($d?$d:$n)."\n"; }
i don't want to use ternary operand nesting.
* take3
flag programming. clearly but long code.
<?php foreach (range(1, 100) as $n) { $b3 = ($n % 3)?0:1; $b5 = ($n % 5)?0:2; switch ($b3 | $b5) { case 0: echo "$n\n"; break; case 1: echo "Fizz\n"; break; case 2: echo "Buzz\n"; break; case 3: echo "FizzBuzz\n"; break; } }
* take3
printf programming.
$fmt = array('%d', 'Fizz', 'Buzz', 'FizzBuzz'); foreach (range(1, 100) as $n) { $b3 = ($n % 3)?0:1; $b5 = ($n % 5)?0:2; printf($fmt[$b3 | $b5]."\n", $n); }
* perl
foreach (1 .. 100) {
$_ = ($_, 'Fizz', 'Buzz', 'FizzBuzz')[2 * !($_ % 3) + !($_ % 5)]."\n"; print
}
very simple....
0 件のコメント:
コメントを投稿