Skip to content

Commit c752a43

Browse files
committed
Solve 'Matrix Transpose' kata
1 parent 0e8f2e5 commit c752a43

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/MatrixTranspose.hs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module MatrixTranspose (transpose) where
2+
3+
-- https://www.codewars.com/kata/52fba2a9adcd10b34300094c/train/haskell
4+
5+
transpose :: [[a]] -> [[a]]
6+
transpose [] = []
7+
transpose xss = map (\x -> map (\y -> xss !! y !! x) [0, 1 .. height - 1]) [0, 1 .. width - 1]
8+
where
9+
height = length xss
10+
width = length . head $ xss

test/MatrixTransposeSpec.hs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module MatrixTransposeSpec where
2+
3+
import MatrixTranspose (transpose)
4+
import Test.Hspec
5+
6+
spec :: Spec
7+
spec = do
8+
describe "Fixed tests" $ do
9+
it "works on the example matrix" $ do
10+
transpose [[1, 2, 3], [4, 5, 6]] `shouldBe` [[1, 4], [2, 5], [3, 6]]
11+
it "works on other fixed inputs" $ do
12+
transpose [[1]] `shouldBe` [[1]]
13+
transpose [[1, 2, 3]] `shouldBe` [[1], [2], [3]]
14+
transpose [[1, 2, 3], [4, 5, 6], [7, 8, 9]] `shouldBe` [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
15+
transpose [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]]
16+
`shouldBe` [[1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0]]

0 commit comments

Comments
 (0)