User:VonHaarberg/sandbox

From Wikipedia, the free encyclopedia

This is a list of notable implementations of the Fizz Buzz game. The task to come up with an implementation for this game is hardly a challenge for routined programmers and therefore often used as screening test in interview situations.

Fizz Buzz implementations by programming language[edit]

JavaScript[edit]

A details version using JavaScript:

// Initialize a string variable for the output
var output = '';

// Count to 100 using i as the counter
for (var i = 1; i <= 100; i++) {

    // If i is not divisible by 3 or 5, append the number itself.
    // Note: In JavaScript, non-zero integers are considered as
    // truthy values, therefore if there's a remainder on both,
    // we append the number instead.
    if (i % 5 && i % 3) {
        output += i + ' ';
    }

    // If i is divisible by 3 with no remainder, append Fizz
    if (i % 3 === 0) {
        output += 'Fizz ';
    } 

    // If i is divisible by 5 with no remainder, append Buzz
    if (i % 5 === 0) {
        output += 'Buzz ';
    }
}

// Print the output to the console
console.log(output);

A shorter version:

var o='';
for (var i=1; i<=100; i++) {
    i%3 || (o+='Fizz ');
    i%5 || (o+='Buzz ');
    !(i%3 && i%5) || (o+=(i+' ')); 
}
console.log(o);

F#[edit]

An implementation in F#

let to_string i = match i%3, i%5 with
    | 0,0 -> "FizzBuzz"
    | 0,_ -> "Fizz"
    | _,0 -> "Buzz"
    | _ -> string i

for i in 1..100 do
    printfn "%s" (to_string i)

C++[edit]

Using template metaprogramming in C++:

// Helper macros to make compiler output a bit more readable, but keep the
// source readable too.
#define List _
#define Integer __

template <int Condition, typename A, typename B>
struct If;

template <typename A, typename B>
struct If<true, A, B> {
  typedef A result;
};

template <typename A, typename B>
struct If<false, A, B> {
  typedef B result;
};

struct Nil {};
struct Fizz {};
struct Buzz {};
struct FizzBuzz {};
template <int N>
struct Integer {
  static const int value = N;
};

template <typename Head, typename Rest>
struct List {
  typedef Head head;
  typedef Rest rest;
};

template <typename Container, typename Value>
struct PushFront {
  typedef List<Value, Container> result;
};

template <int N>
struct IntToFizzBuzz {
  typedef typename If<
      N % 15 == 0, FizzBuzz,
      typename If<N % 3 == 0, Fizz, typename If<N % 5 == 0, Buzz, Integer<N>>::
                                        result>::result>::result result;
};

template <int N>
struct DoFizzBuzz {
  typedef typename PushFront<typename IntToFizzBuzz<N>::result,
                             typename DoFizzBuzz<N - 1>::result>::result result;
};

template <>
struct DoFizzBuzz<0> {
  typedef Nil result;
};

template <typename T>
struct Print;

Print<DoFizzBuzz<100>::result> ___;

// Will print when attempting to compile with gcc 5.4:
// fizzbuzz.cc:61:32: error: aggregate ‘Print<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<_<Nil, __<1> >, __<2> >, Fizz>, __<4> >, Buzz>, Fizz>, __<7> >, __<8> >, Fizz>, Buzz>, __<11> >, Fizz>, __<13> >, __<14> >, FizzBuzz>, __<16> >, __<17> >, Fizz>, __<19> >, Buzz>, Fizz>, __<22> >, __<23> >, Fizz>, Buzz>, __<26> >, Fizz>, __<28> >, __<29> >, FizzBuzz>, __<31> >, __<32> >, Fizz>, __<34> >, Buzz>, Fizz>, __<37> >, __<38> >, Fizz>, Buzz>, __<41> >, Fizz>, __<43> >, __<44> >, FizzBuzz>, __<46> >, __<47> >, Fizz>, __<49> >, Buzz>, Fizz>, __<52> >, __<53> >, Fizz>, Buzz>, __<56> >, Fizz>, __<58> >, __<59> >, FizzBuzz>, __<61> >, __<62> >, Fizz>, __<64> >, Buzz>, Fizz>, __<67> >, __<68> >, Fizz>, Buzz>, __<71> >, Fizz>, __<73> >, __<74> >, FizzBuzz>, __<76> >, __<77> >, Fizz>, __<79> >, Buzz>, Fizz>, __<82> >, __<83> >, Fizz>, Buzz>, __<86> >, Fizz>, __<88> >, __<89> >, FizzBuzz>, __<91> >, __<92> >, Fizz>, __<94> >, Buzz>, Fizz>, __<97> >, __<98> >, Fizz>, Buzz> > ___’ has incomplete type and cannot be defined
//  Print<DoFizzBuzz<100>::result> ___;
//                                 ^

Perl 6[edit]

A FizzBuzzWoof implementation in Perl 6:

my @numbers = (0..1000);
for @numbers -> $x {
    given $x {
        when $x % 105 == 0 {
            say "FizzBuzzWoof"
        }
        when $x % 15 == 0 {
            say "FizzBuzz"
        }
        when $x % 21 == 0 {
            say "FizzWoof"
        }
        when $x % 35 == 0 {
            say "BuzzWoof"
        }
        when $x % 3 == 0 {
            say "Fizz"
        }
        when $x % 5 == 0 {
            say "Buzz"
        }
        when $x % 7 == 0 {
            say "Woof"
        }
        default {
            say $x
        }
    }
}

Python[edit]

As a one-liner in Python:

print ['Fizz'*(i%3<1)+'Buzz'*(i%5<1) or i for i in range(1,101)]

See also[edit]

External links[edit]