diff options
| author | chirvasitua <stuart-little@users.noreply.github.com> | 2021-01-25 08:18:09 -0500 |
|---|---|---|
| committer | chirvasitua <stuart-little@users.noreply.github.com> | 2021-01-25 08:18:09 -0500 |
| commit | 962076bc11f1bb00d0246b57851d941abde52c31 (patch) | |
| tree | db1f66d2479be508c12eba07cb3df78d94372e48 | |
| parent | c2e3d94308259dfeed2f131426c365adb88a9663 (diff) | |
| download | perlweeklychallenge-club-962076bc11f1bb00d0246b57851d941abde52c31.tar.gz perlweeklychallenge-club-962076bc11f1bb00d0246b57851d941abde52c31.tar.bz2 perlweeklychallenge-club-962076bc11f1bb00d0246b57851d941abde52c31.zip | |
1st commit on 097_haskell
| -rwxr-xr-x | challenge-097/stuart-little/haskell/ch-1.hs | 18 | ||||
| -rwxr-xr-x | challenge-097/stuart-little/haskell/ch-2.hs | 22 |
2 files changed, 40 insertions, 0 deletions
diff --git a/challenge-097/stuart-little/haskell/ch-1.hs b/challenge-097/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..6dbe278140 --- /dev/null +++ b/challenge-097/stuart-little/haskell/ch-1.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env runghc + +-- run <script> <quoted string> <number> + +import Data.Char (ord,chr,) +import System.Environment (getArgs,) + +shiftCaps :: Int -> Char -> Char +shiftCaps n c + |elem c ['A'..'Z'] = chr $ ord 'A' + mod ((ord c -ord 'A') - (mod n 26)) 26 + |otherwise =c + +rot :: Int -> String -> String +rot n str = map (shiftCaps n) str + +main = do + (str,shift) <- getArgs >>= return.(\(x:y:_)->(x,(read::String->Int) y)) + putStrLn $ rot shift str diff --git a/challenge-097/stuart-little/haskell/ch-2.hs b/challenge-097/stuart-little/haskell/ch-2.hs new file mode 100755 index 0000000000..d8b0afe2f8 --- /dev/null +++ b/challenge-097/stuart-little/haskell/ch-2.hs @@ -0,0 +1,22 @@ +#!/usr/bin/env runghc + +-- run <script> <binary string> <number> + +import Data.List (transpose,elemIndices) +import Data.List.Extra (maximumOn) +import Data.List.Split (chunksOf) +import System.Environment (getArgs) + +mostOccsTgt :: Eq a => [[a]] -> [a] +mostOccsTgt xss = map (\ys -> maximumOn (length . (flip elemIndices ys)) ys) $ transpose xss + +nrFlips :: Eq a => [a] -> [[a]] -> Int +nrFlips tgt strs = sum $ map (\s -> length $ filter (\(p,q)-> p/=q) $ zip tgt s) strs + +main = do + (bin,nr) <- getArgs >>= return.(\(x:y:_)-> (x,(read::String->Int) y)) + let bins = chunksOf nr bin + tgt = mostOccsTgt bins + putStrLn $ "Initial binary words:\n" ++ (unlines bins) + putStrLn $ "Target string: " ++ tgt + putStrLn $ "Need to flip " ++ (show $ nrFlips tgt bins) ++ " position(s)." |
