all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
12
Task/Spiral-matrix/Haskell/spiral-matrix.hs
Normal file
12
Task/Spiral-matrix/Haskell/spiral-matrix.hs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
module Spiral where
|
||||
|
||||
import Data.List
|
||||
import Control.Monad
|
||||
import Control.Monad.Instances
|
||||
|
||||
grade xs = map snd. sort $ zip xs [0..]
|
||||
values n = cycle [1,n,-1,-n]
|
||||
counts n = (n:).concatMap (ap (:) return) $ [n-1,n-2..1]
|
||||
reshape n = unfoldr (\xs -> if null xs then Nothing else Just (splitAt n xs))
|
||||
|
||||
spiral n = reshape n . grade. scanl1 (+). concat $ zipWith replicate (counts n) (values n)
|
||||
Loading…
Add table
Add a link
Reference in a new issue