bd808.com

FOSS in, FOSS out: software, process and operations

FizzBuzz — the Wrong Way to Do It

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. imranontech [at] googlemail.com

fizzbuzz.py
1
2
3
#!/usr/bin/env python
for i in xrange(1, 101):
  print (not i % 3) * "Fizz" + (not i % 5) * "Buzz" or i
fizzbuzz.php
1
2
3
4
5
6
<?php
$p = "printf"; $r = "str_repeat";
for ($i = 1; $i <= 100; $i++) {
  $p("%s\n", $r($i, $p("%s%s",
      $r("Fizz", !($i % 3)), $r("Buzz", !($i % 5))) == 0));
}
fizzbuzz.sh
1
2
3
4
5
6
7
8
#!/usr/bin/env bash
for i in {1..100}; do
  if   [ 0 = $(($i % 15)) ]; then echo "FizzBuzz";
  elif [ 0 = $(($i %  3)) ]; then echo "Fizz";
  elif [ 0 = $(($i %  5)) ]; then echo "Buzz";
  else                       echo $i;
  fi
done