Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FizzBuzz in hakell #468

Merged
merged 4 commits into from
Aug 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions Bubble_Sort/Haskell/BubbleSort.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module BubbleSort (bubbleSort) where

bubble :: (Ord a) => [a] -> [a]
bubble [] = []
bubble [a] = [a]
bubble (x:y:z) = min x y : bubble ((max x y):z)


bubbleSort :: (Ord a) => [a] -> [a]
bubbleSort [] = []
bubbleSort xs = helpbubble (length xs) xs
where helpbubble 1 xs = bubble xs
helpbubble n xs = helpbubble (n-1) $ bubble xs
27 changes: 27 additions & 0 deletions Bubble_Sort/Haskell/BubbleSortTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import BubbleSort

main = do
putStrLn "Tests for BubbleSort algorithm: "
let results = tests
putStrLn $ "Test array1: " ++ (succesornot $ head results)
putStrLn $ "Test array2: " ++ (succesornot $ (!!) results 1)
putStrLn $ "Test array3: " ++ (succesornot $ last results)
where succesornot = (\x -> if x then "Succeeded!" else "Failed")


array1 :: (Integral a) => [a]
array1 = [99,98..0]
array1' = [0..99]

array2 :: String
array2 = "This is just a string, used to test our BubbleSort algorithm!"
array2' = " !,BSTaabbdeeegghhiiiijllmnoooorrrrsssssstttttttuuuu"

array3 :: [Int]
array3 = [2,45,3,3585,4135,748,6384,358,43865,4,83654,354,658,4,36848,83,7,12,1,24,45,4,54,54,54,5,413,13,54]
array3' = [1,2,3,4,4,4,5,7,12,13,24,45,45,54,54,54,54,83,354,358,413,658,748,3585,4135,6384,36848,43865,83654]

tests :: [Bool]
tests = [bubbleSort array1 == array1'] ++ [bubbleSort array2 == array2'] ++ [bubbleSort array3 == array3']


6 changes: 6 additions & 0 deletions FizzBuzz/Haskell/FizzBuzz.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module FizzBuzz (fizzbuzz) where

fizzbuzz xs =
[if ((x `mod` 3 == 0) && (x `mod` 5 == 0)) then "FizzBuzz" else if (x `mod` 3) == 0 then "Fizz" else if (x `mod` 5) == 0 then "Buzz" else show x | x <- xs]


11 changes: 11 additions & 0 deletions FizzBuzz/Haskell/FizzBuzzTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import FizzBuzz

main = do
putStrLn "Test for FizzBuzz: "
let result = fizzbuzzTest
putStrLn $ "Test: " ++ (succesornot result)
where succesornot = (\x -> if x then "Succeeded!" else "Failed")

fizzbuzzTest :: Bool
fizzbuzzTest = fizzbuzz [1..100] == ["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"]

4 changes: 4 additions & 0 deletions FizzBuzz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:
"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”."
Source: "Using FizzBuzz to Find Developers who Grok Coding" http://tickletux.wordpress.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/
Source: http://c2.com/cgi/wiki?FizzBuzzTest