diff options
| author | chirvasitua <stuart-little@users.noreply.github.com> | 2021-08-09 21:31:33 -0400 |
|---|---|---|
| committer | chirvasitua <stuart-little@users.noreply.github.com> | 2021-08-09 21:31:33 -0400 |
| commit | 9a2d50bfc4d0eea9e36781189154e59179ff46ca (patch) | |
| tree | 0fd697987f4904afee966572ff0bab8933d4bead | |
| parent | dae33f10b00beaf02883597401ede84ddcc3b77f (diff) | |
| download | perlweeklychallenge-club-9a2d50bfc4d0eea9e36781189154e59179ff46ca.tar.gz perlweeklychallenge-club-9a2d50bfc4d0eea9e36781189154e59179ff46ca.tar.bz2 perlweeklychallenge-club-9a2d50bfc4d0eea9e36781189154e59179ff46ca.zip | |
1st commit on 125_haskell
| -rwxr-xr-x | challenge-125/stuart-little/haskell/ch-1.hs | 13 | ||||
| -rwxr-xr-x | challenge-125/stuart-little/haskell/ch-2.hs | 48 |
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-125/stuart-little/haskell/ch-1.hs b/challenge-125/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..f54ca4974e --- /dev/null +++ b/challenge-125/stuart-little/haskell/ch-1.hs @@ -0,0 +1,13 @@ +#!/usr/bin/env runghc + +-- run <script> <number> + +import System.Environment (getArgs) + +trps :: Int -> [[Int]] +trps n = top ++ mid where + top = filter (\[x,y,z] -> x>0 && y>0 && x^2+y^2==z^2) [[i,(floor $ sqrt $ fromIntegral (n^2-i^2)),n] | i <- [1..div (n+1) 2]] + mid = map (\i-> [div ((div (n^2) i)-i) 2, n, div ((div (n^2) i)+i) 2]) $ filter(\i-> mod (n^2) i == 0 && mod i 2 == mod (div (n^2) i) 2) [1..n-1] + +main :: IO () +main = getArgs >>= putStrLn . (\x-> if length x > 0 then x else "-1") . unlines . map unwords . (map.map) show . trps.(read::String->Int).head diff --git a/challenge-125/stuart-little/haskell/ch-2.hs b/challenge-125/stuart-little/haskell/ch-2.hs new file mode 100755 index 0000000000..3d0e17fb25 --- /dev/null +++ b/challenge-125/stuart-little/haskell/ch-2.hs @@ -0,0 +1,48 @@ +#!/usr/bin/env runghc + +import Data.List ((\\), inits) +import Data.List.Extra (maximumOn) +import System.Environment (getArgs) + +lr :: [String] -> [[String]] +lr xs + |(length xs < 3) || (head xs == ".") =[[],[]] + |length xs == 3 =[["."],["."]] + |otherwise = [l, (tail xs) \\ l] where + l = head $ filter (((-1)==).sum.map (\x-> if x == "." then (-1) else 1)) $ inits $ tail xs + +lrLongPath :: [String] -> [[String]] +lrLongPath xs + |head xs == "." =[[],[]] + |length xs == 3 = replicate 2 [head xs] + |otherwise = map ((head xs):) $ map ((maximumOn length).lrLongPath) $ lr xs + +biLongPath :: [String] -> [String] +biLongPath xs + |length xs < 3 || head xs == "." =[] + |length xs == 3 = [head xs] + |otherwise = maximumOn length [path, biLongPath(left), biLongPath(right)] where + [left,right] = lr(xs) + [lPath,rPath] = lrLongPath(xs) + path = (reverse $ tail lPath) ++ rPath + +main :: IO () +main = getArgs >>= putStrLn.unwords.biLongPath + +{- +run <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values> + +ref: https://stackoverflow.com/a/2676849/11064961 + +e.g. 1 2 4 . 7 . . . 3 5 . . 6 . . represents the tree + + 1 + / \ + 2 3 + / / \ + 4 5 6 + \ + 7 + +given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2 +-} |
