Skip to content

Commit

Permalink
Solve 'Matrix Transpose' kata
Browse files Browse the repository at this point in the history
  • Loading branch information
borisskert committed Nov 4, 2024
1 parent 0e8f2e5 commit c752a43
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/MatrixTranspose.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module MatrixTranspose (transpose) where

-- https://www.codewars.com/kata/52fba2a9adcd10b34300094c/train/haskell

transpose :: [[a]] -> [[a]]
transpose [] = []
transpose xss = map (\x -> map (\y -> xss !! y !! x) [0, 1 .. height - 1]) [0, 1 .. width - 1]
where
height = length xss
width = length . head $ xss
16 changes: 16 additions & 0 deletions test/MatrixTransposeSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module MatrixTransposeSpec where

import MatrixTranspose (transpose)
import Test.Hspec

spec :: Spec
spec = do
describe "Fixed tests" $ do
it "works on the example matrix" $ do
transpose [[1, 2, 3], [4, 5, 6]] `shouldBe` [[1, 4], [2, 5], [3, 6]]
it "works on other fixed inputs" $ do
transpose [[1]] `shouldBe` [[1]]
transpose [[1, 2, 3]] `shouldBe` [[1], [2], [3]]
transpose [[1, 2, 3], [4, 5, 6], [7, 8, 9]] `shouldBe` [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
transpose [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]]
`shouldBe` [[1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0]]

0 comments on commit c752a43

Please sign in to comment.