Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/SelectiveArrayReversing.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module SelectiveArrayReversing (selReverse) where
import Data.List.Split (chunksOf)

-- https://www.codewars.com/kata/58f6000bc0ec6451960000fd/train/haskell

selReverse :: Int -> [a] -> [a] -- note argument order
-- selReverse l xs = map (\x -> xs !! x) . take (length xs) $ indices l
selReverse l xs = concatMap reversed . chunksOf l $ xs

indices :: Int -> [Int]
indices n = (map go) $ [0..]
where
go :: Int -> Int
go i = (d + 1) * n - r
where
d = i `div` n
r = i `mod` n + 1

-- bei 3:
-- 0 -> 2 = 3 - 1 = (0 + 1) * 3 - 1
-- 1 -> 1 = 3 - 2 = (0 + 1) * 3 - 2
-- 2 -> 0 = 3 - 3 = (0 + 1) * 3 - 3
-- 3 -> 5 = 6 - 1 = (1 + 1) * 3 - 1
-- 4 -> 4 = 6 - 2 = (1 + 1) * 3 - 2
-- 5 -> 3 = 6 - 3 = (1 + 1) * 3 - 3
-- 6 -> 8 = 9 - 1 = (2 + 1) * 3 - 1
-- 7 -> 7 = 9 - 2 = (2 + 1) * 3 - 2
-- 8 -> 6 = 9 - 3 = (2 + 1) * 3 - 3

reversed :: [a] -> [a]
reversed xs = map (\x -> xs !! x) $ indices (length xs)
10 changes: 10 additions & 0 deletions test/SelectiveArrayReversingSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module SelectiveArrayReversingSpec (spec) where

import SelectiveArrayReversing (selReverse)
import Test.Hspec

spec :: Spec
spec = do
it "example tests" $ do
selReverse 3 [2, 4, 6, 8, 10, 12, 14, 16] `shouldBe` [6, 4, 2, 12, 10, 8, 16, 14]
selReverse 2 [1, 2, 3, 4, 5, 6] `shouldBe` [2, 1, 4, 3, 6, 5]
Loading