Skip to content

Commit 10b702f

Browse files
committed
Solve 'Duck Duck Goose' kata
1 parent 142753c commit 10b702f

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/DuckDuckGoose.hs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module DuckDuckGoose (duckDuckGoose, Player (..)) where
2+
3+
import Preloaded (Player (name))
4+
5+
-- https://www.codewars.com/kata/582e0e592029ea10530009ce/train/haskell
6+
7+
duckDuckGoose :: [Player] -> Int -> String
8+
duckDuckGoose players goose = name . (!! index) $ players
9+
where
10+
count = length players
11+
index = (`mod` count) . subtract 1 $ goose

src/Preloaded.hs

+2
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ data Circle = Circle
3939
{ center :: Point
4040
, radius :: Double
4141
} deriving (Show, Eq)
42+
43+
newtype Player = Player {name :: String} deriving (Show, Eq)

test/DuckDuckGooseSpec.hs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module DuckDuckGooseSpec where
2+
3+
import Test.Hspec
4+
import Test.HUnit
5+
import DuckDuckGoose (duckDuckGoose)
6+
import Preloaded
7+
8+
spec :: Spec
9+
spec = do
10+
describe "Fixed Tests" $ do
11+
it "should find the correct goose" $ do
12+
let players = Player . (:"") <$> "abcdcefghz"
13+
runTest players 1 "a"
14+
runTest players 3 "c"
15+
runTest players 10 "z"
16+
runTest players 20 "z"
17+
runTest players 30 "z"
18+
runTest players 18 "g"
19+
runTest players 28 "g"
20+
runTest players 12 "b"
21+
runTest players 2 "b"
22+
runTest players 7 "f"
23+
24+
runTest :: [Player] -> Int -> String -> Expectation
25+
runTest players goose expected =
26+
assertEqual (showInput players goose) expected $ duckDuckGoose players goose
27+
28+
showInput :: [Player] -> Int -> String
29+
showInput players goose = unwords ["duckDuckGoose", show players, show goose]

0 commit comments

Comments
 (0)