diff options
| -rw-r--r-- | challenge-081/jeongoon/haskell/ch-1.hs | 32 | ||||
| -rw-r--r-- | challenge-081/jeongoon/haskell/ch-2.hs | 59 | ||||
| l--------- | challenge-081/jeongoon/haskell/input | 1 |
3 files changed, 92 insertions, 0 deletions
diff --git a/challenge-081/jeongoon/haskell/ch-1.hs b/challenge-081/jeongoon/haskell/ch-1.hs new file mode 100644 index 0000000000..0b1e046ecd --- /dev/null +++ b/challenge-081/jeongoon/haskell/ch-1.hs @@ -0,0 +1,32 @@ +import System.Environment +import System.Exit + +{- comment: + I tried with Data.Text but it was faster without Data.Text + but ch-2.hs is much faster when using it -} + +--commonDivisors :: (Integral a) => [a] -> [a] +commonDivisors [] = [] +commonDivisors xs = filter (\cd -> all ((==0).(flip rem) cd) xs) [1..(gcd')] + where gcd' = foldr1 (\acc x -> gcd acc x) xs + +--isRepeated :: [Char] -> [Char] -> Bool +isRepeated a inStr = ((foldr1 (++).replicate count) a) == inStr where + lenStr = length inStr + lenA = length a + count = lenStr `div` lenA + +commonBaseStrings :: [String] -> [String] +commonBaseStrings [] = [] +commonBaseStrings [a] = [a] +commonBaseStrings strls = + filter (\cbs' -> (all (cbs' `isRepeated`) strls)) cbsls + where + strA = strls !! 0 + cbsls = (map ((flip take) strA).commonDivisors.map length) strls + +main = do + strs <- getArgs + if length strs < 2 + then die $ "usage: runhaskell ch-1.hs <string> <string>" + else (putStrLn.unwords.commonBaseStrings) strs diff --git a/challenge-081/jeongoon/haskell/ch-2.hs b/challenge-081/jeongoon/haskell/ch-2.hs new file mode 100644 index 0000000000..cb5b8e31d6 --- /dev/null +++ b/challenge-081/jeongoon/haskell/ch-2.hs @@ -0,0 +1,59 @@ +import System.Environment +import System.IO (readFile) +import System.Exit +import qualified Data.Map.Strict as M +import qualified Data.Text as T +import Data.Map.Strict (Map) + +{- test with: cd jeongoon/haskell; runhaskell ./ch-2.hs; cd - +-} + +{- comment: + when using Data.Text, it becomes almost twice faster than regular [Char] + complie with: ghc -O2 ch-2 + tested with ./ch-2 + + but still slower than compiled version of ch-2.go +-} + +frequencyFromList :: [T.Text] -> Map T.Text Integer +frequencyFromList = foldr (\x acc -> M.insertWith (+) x 1 acc) M.empty + +invertedFrequency :: Map T.Text Integer -> Map Integer [T.Text] +invertedFrequency = + (foldr (\(word,freq) acc -> + M.insertWith (++) freq [word] acc) M.empty) . M.toList + +filterStrings :: [T.Text] -> T.Text -> T.Text +filterStrings [] txt = txt +filterStrings wholeFilters txt = filterStrings' T.empty wholeFilters txt where + filterStrings' filtered filters remains + | T.null remains = filtered -- no more string left to filter + -- start over due to nothing filtered at this position + | null filters = filterStrings' filtered' wholeFilters rs + -- apply a filter one by one + | beingFiltered = filterStrings' filtered wholeFilters remains' + + | otherwise = filterStrings' filtered fs remains + where + f = head filters + fs = tail filters + Just (r, rs) = T.uncons remains + filtered' = T.append filtered (T.singleton r) + lenF = T.length f + beingFiltered = (T.take lenF remains) == f + remains' = T.drop lenF remains + +main = do + readFile "input" >>= + return. + M.toList. + invertedFrequency. + frequencyFromList. + T.words. + filterStrings'. + T.pack + >>= + mapM_ (\(frq,wordls) -> + putStrLn $ (show frq) ++ "\t" ++ ((T.unpack . T.unwords) wordls)) + where filterStrings' = filterStrings ((T.words.T.pack) ". , ( ) -- 's \"") diff --git a/challenge-081/jeongoon/haskell/input b/challenge-081/jeongoon/haskell/input new file mode 120000 index 0000000000..8f1f9ce80d --- /dev/null +++ b/challenge-081/jeongoon/haskell/input @@ -0,0 +1 @@ +../perl/input
\ No newline at end of file |
