Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Floyds-triangle/Haskell/floyds-triangle-1.hs
Normal file
32
Task/Floyds-triangle/Haskell/floyds-triangle-1.hs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
--------------------- FLOYDS TRIANGLE --------------------
|
||||
|
||||
floydTriangle :: [[Int]]
|
||||
floydTriangle =
|
||||
( zipWith
|
||||
(fmap (.) enumFromTo <*> (\a b -> pred (a + b)))
|
||||
<$> scanl (+) 1
|
||||
<*> id
|
||||
)
|
||||
[1 ..]
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main = mapM_ (putStrLn . formatFT) [5, 14]
|
||||
|
||||
------------------------- DISPLAY ------------------------
|
||||
|
||||
formatFT :: Int -> String
|
||||
formatFT n = unlines $ unwords . zipWith alignR ws <$> t
|
||||
where
|
||||
t = take n floydTriangle
|
||||
ws = length . show <$> last t
|
||||
|
||||
alignR :: Int -> Int -> String
|
||||
alignR n =
|
||||
( (<>)
|
||||
=<< flip replicate ' '
|
||||
. (-) n
|
||||
. length
|
||||
)
|
||||
. show
|
||||
29
Task/Floyds-triangle/Haskell/floyds-triangle-2.hs
Normal file
29
Task/Floyds-triangle/Haskell/floyds-triangle-2.hs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import Control.Monad ((>=>))
|
||||
import Data.List (mapAccumL)
|
||||
|
||||
--------------------- FLOYD'S TRIANGLE -------------------
|
||||
|
||||
floyd :: Int -> [[Int]]
|
||||
floyd n =
|
||||
snd $
|
||||
mapAccumL
|
||||
(\a x -> ((,) . succ <*> enumFromTo a) (a + x))
|
||||
1
|
||||
[0 .. pred n]
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main = mapM_ putStrLn $ showFloyd . floyd <$> [5, 14]
|
||||
|
||||
showFloyd :: [[Int]] -> String
|
||||
showFloyd x =
|
||||
let padRight n = (drop . length) <*> (replicate n ' ' <>)
|
||||
in unlines
|
||||
( fmap
|
||||
( zipWith
|
||||
(\n v -> padRight n (show v))
|
||||
(fmap (succ . length . show) (last x))
|
||||
>=> id
|
||||
)
|
||||
x
|
||||
)
|
||||
22
Task/Floyds-triangle/Haskell/floyds-triangle-3.hs
Normal file
22
Task/Floyds-triangle/Haskell/floyds-triangle-3.hs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
----------------- LINES OF FLOYDS TRIANGLE ---------------
|
||||
|
||||
floyds :: [[Int]]
|
||||
floyds = iterate floyd [1]
|
||||
|
||||
floyd :: [Int] -> [Int]
|
||||
floyd xs
|
||||
| n < 2 = [1]
|
||||
| otherwise =
|
||||
[ succ (div (n * pred n) 2)
|
||||
.. div (n * succ n) 2
|
||||
]
|
||||
where
|
||||
n = succ (length xs)
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main = do
|
||||
mapM_ print $ take 5 floyds
|
||||
putStrLn ""
|
||||
mapM_ print $ take 14 floyds
|
||||
32
Task/Floyds-triangle/Haskell/floyds-triangle-4.hs
Normal file
32
Task/Floyds-triangle/Haskell/floyds-triangle-4.hs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import Control.Monad (join)
|
||||
import Data.Matrix (Matrix, getElem, matrix, nrows, toLists)
|
||||
|
||||
--------------------- FLOYDS TRIANGLE --------------------
|
||||
|
||||
floyd :: Int -> Matrix (Maybe Int)
|
||||
floyd n = matrix n n go
|
||||
where
|
||||
go (y, x)
|
||||
| x > y = Nothing
|
||||
| otherwise = Just (x + quot (pred y * y) 2)
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main = mapM_ putStrLn $ showFloyd . floyd <$> [5, 14]
|
||||
|
||||
|
||||
------------------------- DISPLAY ------------------------
|
||||
|
||||
showFloyd :: Matrix (Maybe Int) -> String
|
||||
showFloyd m =
|
||||
(unlines . fmap unwords . toLists) $
|
||||
go <$> m
|
||||
where
|
||||
go Nothing = ""
|
||||
go (Just n) = padRight w (show n)
|
||||
Just v = join getElem (nrows m) m
|
||||
w = length (show v)
|
||||
|
||||
padRight :: Int -> String -> String
|
||||
padRight n = (drop . length) <*> (replicate n ' ' <>)
|
||||
Loading…
Add table
Add a link
Reference in a new issue