FizzBuzz Rule ● Write a program that prints the numbers from 1 to 100. ● For multiples of three print “Fizz” instead of the number ● For multiples of five print “Buzz”. ● For numbers which are multiples of both three and five print “FizzBuzz”
maybe_buzz x m = maybe_div is_buzz "Buzz" m is_n:: Integer -> Integer -> Integer where is_buzz = buzz x is_n x y = x `mod` y
maybe_fizzbuzz :: Integer -> IsFizzBuzz -> String fizz :: Integer -> Integer maybe_fizzbuzz x m = case m of fizz x = is_n x 3 Nothing -> show x
Just x -> x buzz :: Integer -> Integer
buzz x = is_n x 5 maybe_process :: Integer -> String
maybe_process x = maybe_fizzbuzz x =<< is_fizzbuzz x maybe_div :: Integer -> String -> IsFizzBuzz -> where is_fizzbuzz x = maybe_fizz x >>= maybe_buzz x IsFizzBuzz maybe_div 0 str m = case m of go :: (Integer -> String) -> IO () Nothing -> Just str go f = process f 1 Just x -> Just $ str ++ x maybe_div _ str m = m process :: (Integer -> String) -> Integer -> IO ()
process f 101 = putStr "" maybe_fizz :: Integer -> IsFizzBuzz -> IsFizzBuzz process f x = (putStrLn $ f x) >> process f next maybe_fizz x m = maybe_div is_fizz "Fizz" m where next = x + 1 where is_fizz = fizz x
Real Pythonista solves the Problem at 3sec.
Basic # begin code for i in range(1, 100): if (i % 15 == 0): print "FizzBuzz" if (i % 3 == 0): print "Buzz" if (i % 5 == 0): print "Fizz" print i # end
“Are You kidding me ?”
Real Pythonista don’t use “if”statement in FizzBuzz.