Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Spiral-matrix/Haskell/spiral-matrix-1.hs
Normal file
9
Task/Spiral-matrix/Haskell/spiral-matrix-1.hs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import Data.List
|
||||
import Control.Monad
|
||||
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)
|
||||
displayRow = putStrLn . intercalate " " . map show
|
||||
main = mapM displayRow $ spiral 5
|
||||
9
Task/Spiral-matrix/Haskell/spiral-matrix-2.hs
Normal file
9
Task/Spiral-matrix/Haskell/spiral-matrix-2.hs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import Data.List
|
||||
import Control.Applicative
|
||||
counts = tail . reverse . concat . map (replicate 2) . enumFromTo 1
|
||||
values = cycle . ((++) <$> map id <*> map negate) . (1 :) . (: [])
|
||||
grade = map snd . sort . flip zip [0..]
|
||||
copies = grade . scanl1 (+) . concat . map (uncurry replicate) . (zip <$> counts <*> values)
|
||||
parts = (<*>) take $ (.) <$> (map . take) <*> (iterate . drop) <*> copies
|
||||
disp = (>> return ()) . mapM (putStrLn . intercalate " " . map show) . parts
|
||||
main = disp 5
|
||||
10
Task/Spiral-matrix/Haskell/spiral-matrix-3.hs
Normal file
10
Task/Spiral-matrix/Haskell/spiral-matrix-3.hs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import Data.List (transpose)
|
||||
import Text.Printf (printf)
|
||||
|
||||
-- spiral is the first row plus a smaller spiral rotated 90 deg
|
||||
spiral 0 _ _ = [[]]
|
||||
spiral h w s = [s .. s+w-1] : rot90 (spiral w (h-1) (s+w))
|
||||
where rot90 = (map reverse).transpose
|
||||
|
||||
-- this is sort of hideous, someone may want to fix it
|
||||
main = mapM_ (\row->mapM_ ((printf "%4d").toInteger) row >> putStrLn "") (spiral 10 9 1)
|
||||
30
Task/Spiral-matrix/Haskell/spiral-matrix-4.hs
Normal file
30
Task/Spiral-matrix/Haskell/spiral-matrix-4.hs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import Data.List (intercalate, transpose)
|
||||
|
||||
---------------------- SPIRAL MATRIX ---------------------
|
||||
spiral :: Int -> [[Int]]
|
||||
spiral n = go n n 0
|
||||
where
|
||||
go rows cols x
|
||||
| 0 < rows =
|
||||
[x .. pred cols + x] :
|
||||
fmap
|
||||
reverse
|
||||
(transpose $ go cols (pred rows) (x + cols))
|
||||
| otherwise = [[]]
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main = putStrLn $ wikiTable $ spiral 5
|
||||
|
||||
|
||||
--------------------- TABLE FORMATTING -------------------
|
||||
wikiTable :: Show a => [[a]] -> String
|
||||
wikiTable =
|
||||
concat
|
||||
. ("{| class=\"wikitable\" style=\"text-align: right;" :)
|
||||
. ("width:12em;height:12em;table-layout:fixed;\"\n|-\n" :)
|
||||
. return
|
||||
. (<> "\n|}")
|
||||
. intercalate "\n|-\n"
|
||||
. fmap (('|' :) . (' ' :) . intercalate " || " . fmap show)
|
||||
Loading…
Add table
Add a link
Reference in a new issue