aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-18 13:45:45 +0100
committerGitHub <noreply@github.com>2021-05-18 13:45:45 +0100
commit63a118d11481e542faf1fe0399ae4b1531748b6f (patch)
tree4bf1244943d7f1a8e13eba8d24ab1f4395f243c8
parent81d709a2aa3f88844414d2cadaf2728f964e291a (diff)
parent0c1ba5475d069602ac737962f00f414aae5193b7 (diff)
downloadperlweeklychallenge-club-63a118d11481e542faf1fe0399ae4b1531748b6f.tar.gz
perlweeklychallenge-club-63a118d11481e542faf1fe0399ae4b1531748b6f.tar.bz2
perlweeklychallenge-club-63a118d11481e542faf1fe0399ae4b1531748b6f.zip
Merge pull request #4104 from stuart-little/stuart-little_113_haskell
1st commit on 113_haskell
-rwxr-xr-xchallenge-113/stuart-little/haskell/ch-1.hs20
-rwxr-xr-xchallenge-113/stuart-little/haskell/ch-2.hs42
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-113/stuart-little/haskell/ch-1.hs b/challenge-113/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..2de27e6a9b
--- /dev/null
+++ b/challenge-113/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,20 @@
+#!/usr/bin/env runghc
+
+-- run <script> <digit>
+
+import System.Environment (getArgs)
+
+lastDigSumm :: Int -> Int -> Int -> Bool
+lastDigSumm nr dig nrSummands = (mod (nr - nrSummands * dig) 10 == 0) && (nrSummands * dig <= nr) && (nrSummands * ((dig -1) * 10 + dig) >= nr)
+
+lastDig :: Int -> Int -> Bool
+lastDig nr dig = not $ null $ filter (lastDigSumm nr dig) [1..9]
+
+sol :: Int -> Int -> Bool
+sol nr dig
+ |dig == 0 = (nr >= 101) || (mod nr 10 == 0)
+ |otherwise = (nr >= dig*11) || lastDig nr dig
+
+main = do
+ [nr,dig] <- getArgs >>= return . map (read::String->Int) . take 2
+ putStrLn . show . fromEnum $ sol nr dig
diff --git a/challenge-113/stuart-little/haskell/ch-2.hs b/challenge-113/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..62e39d6f99
--- /dev/null
+++ b/challenge-113/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,42 @@
+#!/usr/bin/env runghc
+
+import Data.Char (isDigit)
+import Data.List ((\\), inits)
+import Data.Tree (Tree, drawTree, unfoldTree)
+import System.Environment (getArgs)
+
+sumMinNrs :: [String] -> [String]
+sumMinNrs xs = map (\x -> if (all isDigit x) then show (sm - ((read::String->Int)) x) else x) xs where
+ sm = sum $ map (read::String->Int) $ filter (all isDigit) xs
+
+split2trees :: [String] -> ([String],[String])
+split2trees xs = (lhalf, xs \\ lhalf) where
+ lhalf = head $ dropWhile (\ys -> 2*(length $ filter (\s -> s==".") ys) <= length ys ) $ inits xs
+
+nodeIter :: [String] -> (String, [[String]])
+nodeIter xs = if (head xs == ".") then ("",[]) else (head xs, [lchild,rchild]) where
+ (lchild,rchild) = split2trees $ tail xs
+
+mkTree :: [String] -> Tree String
+mkTree = unfoldTree nodeIter
+
+main = do
+ outTreeList <- getArgs >>= return . sumMinNrs . (\x -> if (not $ null x) then x else ["1", "2", "4", ".", "7", ".", ".", ".", "3", "5", ".", ".", "6", ".", "."])
+ putStrLn $ drawTree $ mkTree outTreeList
+{--
+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
+--}