clojure
module Solution where fizzbuzz :: [String] -> Int -> Int -> [String] fizzbuzz acc i n | n < i = acc | otherwise = fizzbuzz (s : acc) i (n - 1) where s = case (mod n 3, mod n 5) of (0, 0) -> "fizzbuzz" (0, _) -> "fizz" (_, 0) -> "buzz" _ -> show n
def rec(acc,i,n):done=i>nmatch done:case True:return acccase _:fb=not (i%3 or i%5)f=not i%3b=not i%5match fb:case True:return rec(acc+["fizzbuzz"],i+1,n)case _:match f:case True:return rec(acc+["fizz"],i+1,n)case _:match b:case True:return rec(acc+["buzz"],i+1,n)case _:return rec(acc+[str(i)],i+1,n)def fizzbuzz(n):return rec([],1,n)# def fizzbuzz(n):# acc=[]# i=1# while i<=n:# if not (i%3 or i%5):# acc.append("fizzbuzz")# elif not i%3:# acc.append("fizz")# elif not i%5:# acc.append("buzz")# else:# acc.append(str(i))# i+=1# return acc- module Solution where
- fizzbuzz :: [String] -> Int -> Int -> [String]
- fizzbuzz acc i n
- | n < i = acc
- | otherwise = fizzbuzz (s : acc) i (n - 1)
- where
- s = case (mod n 3, mod n 5) of
- (0, 0) -> "fizzbuzz"
- (0, _) -> "fizz"
- (_, 0) -> "buzz"
- _ -> show n
module TestSpec (spec) where import Solution import Test.Hspec import Test.QuickCheck import Control.Exception (evaluate) spec :: Spec spec = do it "works with 0" $ do fizzbuzz [] 0 0 `shouldBe` ["fizzbuzz"] it "works with 1" $ do fizzbuzz [] 0 1 `shouldBe` ["fizzbuzz", "1"] it "works with 11" $ do fizzbuzz [] 0 11 `shouldBe` ["fizzbuzz", "1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", "buzz", "11"] it "works with 512" $ do fizzbuzz [] 0 512 `shouldBe` ["fizzbuzz","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","101","fizz","103","104","fizzbuzz","106","107","fizz","109","buzz","fizz","112","113","fizz","buzz","116","fizz","118","119","fizzbuzz","121","122","fizz","124","buzz","fizz","127","128","fizz","buzz","131","fizz","133","134","fizzbuzz","136","137","fizz","139","buzz","fizz","142","143","fizz","buzz","146","fizz","148","149","fizzbuzz","151","152","fizz","154","buzz","fizz","157","158","fizz","buzz","161","fizz","163","164","fizzbuzz","166","167","fizz","169","buzz","fizz","172","173","fizz","buzz","176","fizz","178","179","fizzbuzz","181","182","fizz","184","buzz","fizz","187","188","fizz","buzz","191","fizz","193","194","fizzbuzz","196","197","fizz","199","buzz","fizz","202","203","fizz","buzz","206","fizz","208","209","fizzbuzz","211","212","fizz","214","buzz","fizz","217","218","fizz","buzz","221","fizz","223","224","fizzbuzz","226","227","fizz","229","buzz","fizz","232","233","fizz","buzz","236","fizz","238","239","fizzbuzz","241","242","fizz","244","buzz","fizz","247","248","fizz","buzz","251","fizz","253","254","fizzbuzz","256","257","fizz","259","buzz","fizz","262","263","fizz","buzz","266","fizz","268","269","fizzbuzz","271","272","fizz","274","buzz","fizz","277","278","fizz","buzz","281","fizz","283","284","fizzbuzz","286","287","fizz","289","buzz","fizz","292","293","fizz","buzz","296","fizz","298","299","fizzbuzz","301","302","fizz","304","buzz","fizz","307","308","fizz","buzz","311","fizz","313","314","fizzbuzz","316","317","fizz","319","buzz","fizz","322","323","fizz","buzz","326","fizz","328","329","fizzbuzz","331","332","fizz","334","buzz","fizz","337","338","fizz","buzz","341","fizz","343","344","fizzbuzz","346","347","fizz","349","buzz","fizz","352","353","fizz","buzz","356","fizz","358","359","fizzbuzz","361","362","fizz","364","buzz","fizz","367","368","fizz","buzz","371","fizz","373","374","fizzbuzz","376","377","fizz","379","buzz","fizz","382","383","fizz","buzz","386","fizz","388","389","fizzbuzz","391","392","fizz","394","buzz","fizz","397","398","fizz","buzz","401","fizz","403","404","fizzbuzz","406","407","fizz","409","buzz","fizz","412","413","fizz","buzz","416","fizz","418","419","fizzbuzz","421","422","fizz","424","buzz","fizz","427","428","fizz","buzz","431","fizz","433","434","fizzbuzz","436","437","fizz","439","buzz","fizz","442","443","fizz","buzz","446","fizz","448","449","fizzbuzz","451","452","fizz","454","buzz","fizz","457","458","fizz","buzz","461","fizz","463","464","fizzbuzz","466","467","fizz","469","buzz","fizz","472","473","fizz","buzz","476","fizz","478","479","fizzbuzz","481","482","fizz","484","buzz","fizz","487","488","fizz","buzz","491","fizz","493","494","fizzbuzz","496","497","fizz","499","buzz","fizz","502","503","fizz","buzz","506","fizz","508","509","fizzbuzz","511","512"]
import codewars_test as test# TODO Write testsimport solution # or from solution import example- module TestSpec (spec) where
# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(1 + 1, 2)test.assert_equals(fizzbuzz(0), [])test.assert_equals(fizzbuzz(1), ['1'])test.assert_equals(fizzbuzz(11), ['1', '2', 'fizz', '4', 'buzz', 'fizz', '7', '8', 'fizz', 'buzz', '11'])- import Solution
- import Test.Hspec
- import Test.QuickCheck
- import Control.Exception (evaluate)
- spec :: Spec
- spec = do
- it "works with 0" $ do
- fizzbuzz [] 0 0 `shouldBe` ["fizzbuzz"]
- it "works with 1" $ do
- fizzbuzz [] 0 1 `shouldBe` ["fizzbuzz", "1"]
- it "works with 11" $ do
- fizzbuzz [] 0 11 `shouldBe` ["fizzbuzz", "1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", "buzz", "11"]
- it "works with 512" $ do
- fizzbuzz [] 0 512 `shouldBe` ["fizzbuzz","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","101","fizz","103","104","fizzbuzz","106","107","fizz","109","buzz","fizz","112","113","fizz","buzz","116","fizz","118","119","fizzbuzz","121","122","fizz","124","buzz","fizz","127","128","fizz","buzz","131","fizz","133","134","fizzbuzz","136","137","fizz","139","buzz","fizz","142","143","fizz","buzz","146","fizz","148","149","fizzbuzz","151","152","fizz","154","buzz","fizz","157","158","fizz","buzz","161","fizz","163","164","fizzbuzz","166","167","fizz","169","buzz","fizz","172","173","fizz","buzz","176","fizz","178","179","fizzbuzz","181","182","fizz","184","buzz","fizz","187","188","fizz","buzz","191","fizz","193","194","fizzbuzz","196","197","fizz","199","buzz","fizz","202","203","fizz","buzz","206","fizz","208","209","fizzbuzz","211","212","fizz","214","buzz","fizz","217","218","fizz","buzz","221","fizz","223","224","fizzbuzz","226","227","fizz","229","buzz","fizz","232","233","fizz","buzz","236","fizz","238","239","fizzbuzz","241","242","fizz","244","buzz","fizz","247","248","fizz","buzz","251","fizz","253","254","fizzbuzz","256","257","fizz","259","buzz","fizz","262","263","fizz","buzz","266","fizz","268","269","fizzbuzz","271","272","fizz","274","buzz","fizz","277","278","fizz","buzz","281","fizz","283","284","fizzbuzz","286","287","fizz","289","buzz","fizz","292","293","fizz","buzz","296","fizz","298","299","fizzbuzz","301","302","fizz","304","buzz","fizz","307","308","fizz","buzz","311","fizz","313","314","fizzbuzz","316","317","fizz","319","buzz","fizz","322","323","fizz","buzz","326","fizz","328","329","fizzbuzz","331","332","fizz","334","buzz","fizz","337","338","fizz","buzz","341","fizz","343","344","fizzbuzz","346","347","fizz","349","buzz","fizz","352","353","fizz","buzz","356","fizz","358","359","fizzbuzz","361","362","fizz","364","buzz","fizz","367","368","fizz","buzz","371","fizz","373","374","fizzbuzz","376","377","fizz","379","buzz","fizz","382","383","fizz","buzz","386","fizz","388","389","fizzbuzz","391","392","fizz","394","buzz","fizz","397","398","fizz","buzz","401","fizz","403","404","fizzbuzz","406","407","fizz","409","buzz","fizz","412","413","fizz","buzz","416","fizz","418","419","fizzbuzz","421","422","fizz","424","buzz","fizz","427","428","fizz","buzz","431","fizz","433","434","fizzbuzz","436","437","fizz","439","buzz","fizz","442","443","fizz","buzz","446","fizz","448","449","fizzbuzz","451","452","fizz","454","buzz","fizz","457","458","fizz","buzz","461","fizz","463","464","fizzbuzz","466","467","fizz","469","buzz","fizz","472","473","fizz","buzz","476","fizz","478","479","fizzbuzz","481","482","fizz","484","buzz","fizz","487","488","fizz","buzz","491","fizz","493","494","fizzbuzz","496","497","fizz","499","buzz","fizz","502","503","fizz","buzz","506","fizz","508","509","fizzbuzz","511","512"]