We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Easy haskell solution using the Data.Sequence interface. It uses the fact that the next row in pascal's triangle can be computed as: zipWith (+) (0:previous) (previous ++ [0]). Since accesing last element is not optimal solution for lists, Sequence interface fits better I think.
{-# language OverloadedLists #-}
import Data.Sequence (Seq (..), (<|), (|>))
import qualified Data.Sequence as S
import Data.Foldable (toList)
pascal :: Int -> Seq (Seq Int)
pascal n = S.iterateN n pascal' [1]
where pascal' s = S.zipWith (+) (0 <| s) (s |> 0)
main :: IO ()
main = do
n <- readLn
let pasc = toList $ pascal n
putStrLn `$ unlines $` map (unwords . map show . toList) $ pasc
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Pascal's Triangle
You are viewing a single comment's thread. Return to all comments →
Easy haskell solution using the
Data.Sequence
interface. It uses the fact that the next row in pascal's triangle can be computed as:zipWith (+) (0:previous) (previous ++ [0])
. Since accesing last element is not optimal solution for lists,Sequence
interface fits better I think.