-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c752a43
commit b439d33
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,2,3,4,5,5,5,5,4,3,2,1],[1,2,3,4,5,6,6,5,4,3,2,1], | ||
[1,2,3,4,5,6,6,5,4,3,2,1],[1,2,3,4,5,6,6,5,4,3,2,1], | ||
[1,2,3,4,5,6,6,5,4,3,2,1],[1,2,3,4,5,6,6,5,4,3,2,1], | ||
[1,2,3,4,5,5,5,5,4,3,2,1],[1,2,3,4,4,4,4,4,4,3,2,1], | ||
[1,2,3,3,3,3,3,3,3,3,2,1],[1,2,2,2,2,2,2,2,2,2,2,1], | ||
[1,1,1,1,1,1,1,1,1,1,1,1] ] | ||
(createBox 12 15) | ||
assertEqual "createBox 21 11" | ||
[ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], | ||
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1], | ||
[1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,1], | ||
[1,2,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,3,2,1], | ||
[1,2,3,4,5,5,5,5,5,5,5,5,5,5,5,5,5,4,3,2,1], | ||
[1,2,3,4,5,6,6,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,5,5,4,3,2,1], | ||
[1,2,3,4,4,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,3,3,2,1], | ||
[1,2,2,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,1,1]] | ||
(createBox 21 11) | ||
assertEqual "createBox 19 19" | ||
[ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], | ||
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1], | ||
[1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,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,4,5,5,5,5,5,5,5,5,5,5,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,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 |