From b2d6f0e28dac922ac4ed5c55f79d0dd38f4765d2 Mon Sep 17 00:00:00 2001 From: Determinant Date: Sun, 28 May 2017 19:08:02 -0400 Subject: ... --- 1-10/t.hs | 2 -- 11-20/11.hs | 14 ++++++++++++++ 11-20/12.hs | 6 ++++++ 11-20/13.hs | 10 ++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) delete mode 100644 1-10/t.hs create mode 100644 11-20/11.hs create mode 100644 11-20/12.hs create mode 100644 11-20/13.hs diff --git a/1-10/t.hs b/1-10/t.hs deleted file mode 100644 index 6a4448d..0000000 --- a/1-10/t.hs +++ /dev/null @@ -1,2 +0,0 @@ -grp [] = [] -grp (x:xs) = (x:(filter (==x) xs)):(grp $ filter (/=x) xs) diff --git a/11-20/11.hs b/11-20/11.hs new file mode 100644 index 0000000..a64fdc2 --- /dev/null +++ b/11-20/11.hs @@ -0,0 +1,14 @@ +pack :: Eq a => [a] -> [[a]] + +pack [] = [] +pack [x] = [[x]] +pack (x:xs) = if x == head y then (x:y):ys else [x]:(y:ys) where (y:ys) = pack xs + +data ListItem a = Single a | Multiple Int a deriving Show +encodeModified :: Eq a => [a] -> [ListItem a] + +encodeModified xs = map (\x -> let l = length x + h = head x in + case l of + 1 -> Single h + otherwise -> Multiple l h) $ pack xs diff --git a/11-20/12.hs b/11-20/12.hs new file mode 100644 index 0000000..3c1ce90 --- /dev/null +++ b/11-20/12.hs @@ -0,0 +1,6 @@ +data ListItem a = Single a | Multiple Int a deriving Show +decodeModified :: [ListItem a] -> [a] + +decodeModified [] = [] +decodeModified (Single x:xs) = x:(decodeModified xs) +decodeModified (Multiple l x:xs) = replicate l x ++ decodeModified xs diff --git a/11-20/13.hs b/11-20/13.hs new file mode 100644 index 0000000..db5509e --- /dev/null +++ b/11-20/13.hs @@ -0,0 +1,10 @@ +data ListItem a = Single a | Multiple Int a deriving Show +encodeDirect :: Eq a => [a] -> [ListItem a] + +encodeDirect [] = [] +encodeDirect [x] = [Single x] +encodeDirect (x:xs) = if x == h then n:ys else (Single x):(y:ys) + where (y:ys) = encodeDirect xs + helper (Single z) = (Multiple 2 z, z) + helper (Multiple c z) = (Multiple (c + 1) z, z) + (n, h) = helper y -- cgit v1.2.3-70-g09d2