aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-20 06:26:43 +0000
committerGitHub <noreply@github.com>2021-01-20 06:26:43 +0000
commit51185097fa9b36a31a6b50048fa6ec8739a02456 (patch)
tree3509f1969dcf77bf5e4b18126199201857c0a99b
parentcb83b18301fe082b8413d38a862fac00d960f762 (diff)
parent230cf5521f4ca809bb64de2c927f15f5f190393d (diff)
downloadperlweeklychallenge-club-51185097fa9b36a31a6b50048fa6ec8739a02456.tar.gz
perlweeklychallenge-club-51185097fa9b36a31a6b50048fa6ec8739a02456.tar.bz2
perlweeklychallenge-club-51185097fa9b36a31a6b50048fa6ec8739a02456.zip
Merge pull request #3325 from stuart-little/stuart-little_015_haskell
1st commit on 015_haskell
-rwxr-xr-xchallenge-015/stuart-little/haskell/ch-1.hs15
-rwxr-xr-xchallenge-015/stuart-little/haskell/ch-2.hs30
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-015/stuart-little/haskell/ch-1.hs b/challenge-015/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..b28f56137d
--- /dev/null
+++ b/challenge-015/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,15 @@
+#!/usr/bin/env runghc
+
+-- run <script>
+
+import Data.Numbers.Primes (primes,)
+import Data.List (transpose,)
+
+sprimes = map (\(x:y:z:_) -> y) $ filter (\(x:y:z:_)-> x+z < 2*y) $ transpose $ take 3 $ iterate tail primes
+
+wprimes = map (\(x:y:z:_) -> y) $ filter (\(x:y:z:_)-> x+z > 2*y) $ transpose $ take 3 $ iterate tail primes
+
+main = do
+ let n = 10
+ putStrLn ("Strong primes: " ++ (unwords $ map show $ take n sprimes))
+ putStrLn ("Weak primes: " ++ (unwords $ map show $ take n wprimes))
diff --git a/challenge-015/stuart-little/haskell/ch-2.hs b/challenge-015/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..ecafca94c8
--- /dev/null
+++ b/challenge-015/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,30 @@
+#!/usr/bin/env runghc
+
+-- run <script> <'-e' or '-d' flag tencode or decode respectively> <keyword> <quoted text>
+
+{-# LANGUAGE PackageImports #-}
+
+import Data.Char (toUpper,ord,chr,)
+import System.Environment (getArgs,)
+import "ghc" Util (nTimes,)
+
+alph :: String
+alph = ['A'..'Z']
+
+baseOffset :: Int
+baseOffset = ord 'A'
+
+norm :: String -> String
+norm = (filter (flip elem alph)) . (map toUpper)
+
+vigEncode :: String -> String -> String
+vigEncode key input = map (\(o,c) -> chr $ baseOffset + (mod (ord c -baseOffset +o) (length alph))) $ zip offsets $ norm input where
+ offsets = map ((flip (-) baseOffset) . ord) $ cycle $ norm key
+
+vigDecode :: String -> String -> String
+vigDecode key = nTimes (length alph -1) (vigEncode key)
+
+main = do
+ (flag:key:txt:_) <- getArgs
+ let process = if flag == "-d" then vigDecode else vigEncode
+ putStrLn $ process key txt