diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-26 14:44:22 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-26 14:44:22 +0000 |
| commit | 50580ba8914df4a7cf8d80fd43e5ef971cd4aec3 (patch) | |
| tree | c35ac89c0ba79dc892c10ee4fc7e8d2e7652a133 | |
| parent | 3d3900a2f0f69c54a34683e4e1b5da007b4af9d9 (diff) | |
| parent | 962076bc11f1bb00d0246b57851d941abde52c31 (diff) | |
| download | perlweeklychallenge-club-50580ba8914df4a7cf8d80fd43e5ef971cd4aec3.tar.gz perlweeklychallenge-club-50580ba8914df4a7cf8d80fd43e5ef971cd4aec3.tar.bz2 perlweeklychallenge-club-50580ba8914df4a7cf8d80fd43e5ef971cd4aec3.zip | |
Merge pull request #3375 from stuart-little/stuart-little_097_haskell
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)." |
