Skip to content

Commit 32f435b

Browse files
committed
Solve 'Matrix creation' kata
1 parent 98c5178 commit 32f435b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/MatrixCreation.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module MatrixCreation (getMatrix) where
2+
3+
-- https://www.codewars.com/kata/5a34da5dee1aae516d00004a/train/haskell
4+
5+
getMatrix :: Int -> [[Int]]
6+
getMatrix x = map makeRow [0 .. x - 1]
7+
where
8+
makeRow n = replicate n 0 ++ [1] ++ replicate (x - n - 1) 0

test/MatrixCreationSpec.hs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module MatrixCreationSpec (spec) where
2+
3+
import MatrixCreation (getMatrix)
4+
import Test.HUnit (assertEqual)
5+
import Test.Hspec
6+
7+
spec :: Spec
8+
spec = do
9+
it "example tests" $ do
10+
assertEqual "getMatrix 0" [] $ getMatrix 0
11+
assertEqual "getMatrix 1" [[1]] $ getMatrix 1
12+
assertEqual
13+
"getMatrix 2"
14+
[ [1, 0],
15+
[0, 1]
16+
]
17+
$ getMatrix 2
18+
assertEqual
19+
"getMatrix 5"
20+
[ [1, 0, 0, 0, 0],
21+
[0, 1, 0, 0, 0],
22+
[0, 0, 1, 0, 0],
23+
[0, 0, 0, 1, 0],
24+
[0, 0, 0, 0, 1]
25+
]
26+
$ getMatrix 5

0 commit comments

Comments
 (0)