Skip to content

Commit

Permalink
Solve 'The 'spiraling' box' kata
Browse files Browse the repository at this point in the history
  • Loading branch information
borisskert committed Nov 9, 2024
1 parent c752a43 commit b439d33
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Spiral.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Spiral (createBox) where

-- https://www.codewars.com/kata/63b84f54693cb10065687ae5/train/haskell

createBox :: Int -> Int -> [[Int]]
createBox m = map (`makeFlatRow` m) . makeRow

makeFlatRow :: Int -> Int -> [Int]
makeFlatRow max = map min . makeRow
where
min x
| x >= max = max
| otherwise = x

makeRow :: Int -> [Int]
makeRow x
| even x = row ++ reverse row
| otherwise = row ++ [half + 1] ++ reverse row
where
half = x `div` 2
row = [1 .. half]
76 changes: 76 additions & 0 deletions test/SpiralSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module SpiralSpec where

import Test.Hspec
import Test.HUnit
import Spiral (createBox)

spec :: Spec
spec = do
describe "Fixed Tests" $ do
it "Regular Cases" $ do
assertEqual "createBox 5 8"
[ [1,1,1,1,1],[1,2,2,2,1],[1,2,3,2,1],
[1,2,3,2,1],[1,2,3,2,1],[1,2,3,2,1],
[1,2,2,2,1],[1,1,1,1,1] ]
(createBox 5 8)
assertEqual "createBox 10 9"
[ [1,1,1,1,1,1,1,1,1,1],[1,2,2,2,2,2,2,2,2,1],
[1,2,3,3,3,3,3,3,2,1],[1,2,3,4,4,4,4,3,2,1],
[1,2,3,4,5,5,4,3,2,1],[1,2,3,4,4,4,4,3,2,1],
[1,2,3,3,3,3,3,3,2,1],[1,2,2,2,2,2,2,2,2,1],
[1,1,1,1,1,1,1,1,1,1] ]
(createBox 10 9)
assertEqual "createBox 12 15"
[ [1,1,1,1,1,1,1,1,1,1,1,1],[1,2,2,2,2,2,2,2,2,2,2,1],
[1,2,3,3,3,3,3,3,3,3,2,1],[1,2,3,4,4,4,4,4,4,3,2,1],
[1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,3,4,5,6,6,6,6,6,6,6,6,6,5,4,3,2,1],
[1,2,3,4,5,6,7,7,7,7,7,7,7,6,5,4,3,2,1],
[1,2,3,4,5,6,7,8,8,8,8,8,7,6,5,4,3,2,1],
[1,2,3,4,5,6,7,8,9,9,9,8,7,6,5,4,3,2,1],
[1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1],
[1,2,3,4,5,6,7,8,9,9,9,8,7,6,5,4,3,2,1],
[1,2,3,4,5,6,7,8,8,8,8,8,7,6,5,4,3,2,1],
[1,2,3,4,5,6,7,7,7,7,7,7,7,6,5,4,3,2,1],
[1,2,3,4,5,6,6,6,6,6,6,6,6,6,5,4,3,2,1],
[1,2,3,4,5,5,5,5,5,5,5,5,5,5,5,4,3,2,1],
[1,2,3,4,4,4,4,4,4,4,4,4,4,4,4,4,3,2,1],
[1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,1],
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]
(createBox 19 19)
assertEqual "createBox 2 20"
[ [1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],
[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],[1,1],
[1,1],[1,1],[1,1],[1,1]]
(createBox 2 20)
assertEqual "createBox 2 2" [[1,1],[1,1]] $ createBox 2 2
it "Edge Cases" $ do
assertEqual "createBox 1 6" [[1],[1],[1],[1],[1],[1]] $ createBox 1 6
assertEqual "createBox 10 1" [[1,1,1,1,1,1,1,1,1,1]] $ createBox 10 1
assertEqual "createBox 1 1" [[1]] $ createBox 1 1

0 comments on commit b439d33

Please sign in to comment.