From a6e4866f22618380ca6f3b47445b601dbc4d9984 Mon Sep 17 00:00:00 2001 From: SinTan1729 Date: Fri, 24 May 2024 18:50:16 -0500 Subject: [PATCH] new: Solved problems 11 to 20 --- Problems 11-20/problem_11.hs | 20 ++++++++++++++++++++ Problems 11-20/problem_12.hs | 8 ++++++++ Problems 11-20/problem_13.hs | 21 +++++++++++++++++++++ Problems 11-20/problem_14.hs | 2 ++ Problems 11-20/problem_15.hs | 2 ++ Problems 11-20/problem_16.hs | 2 ++ Problems 11-20/problem_17.hs | 5 +++++ Problems 11-20/problem_18.hs | 4 ++++ Problems 11-20/problem_19.hs | 5 +++++ Problems 11-20/problem_20.hs | 5 +++++ 10 files changed, 74 insertions(+) create mode 100644 Problems 11-20/problem_11.hs create mode 100644 Problems 11-20/problem_12.hs create mode 100644 Problems 11-20/problem_13.hs create mode 100644 Problems 11-20/problem_14.hs create mode 100644 Problems 11-20/problem_15.hs create mode 100644 Problems 11-20/problem_16.hs create mode 100644 Problems 11-20/problem_17.hs create mode 100644 Problems 11-20/problem_18.hs create mode 100644 Problems 11-20/problem_19.hs create mode 100644 Problems 11-20/problem_20.hs diff --git a/Problems 11-20/problem_11.hs b/Problems 11-20/problem_11.hs new file mode 100644 index 0000000..ca99c4d --- /dev/null +++ b/Problems 11-20/problem_11.hs @@ -0,0 +1,20 @@ +data ListItem a = Single a | Multiple Int a deriving (Show) + +getListItem :: (Int, a) -> ListItem a +getListItem (1, x) = Single x +getListItem (n, x) = Multiple n x + +encode :: (Eq a) => [a] -> [(Int, a)] +encode = + foldr + ( \x acc -> case acc of + [] -> [(1, x)] + (hd@(hd_n, hd_a) : tl) -> + if x == hd_a + then (hd_n + 1, hd_a) : tl + else (1, x) : hd : tl + ) + [] + +encodeModified :: (Eq a) => [a] -> [ListItem a] +encodeModified = map getListItem <$> encode diff --git a/Problems 11-20/problem_12.hs b/Problems 11-20/problem_12.hs new file mode 100644 index 0000000..9c2d503 --- /dev/null +++ b/Problems 11-20/problem_12.hs @@ -0,0 +1,8 @@ +data ListItem a = Single a | Multiple Int a deriving (Read) + +fromListItem :: ListItem a -> [a] +fromListItem (Single x) = [x] +fromListItem (Multiple n x) = replicate n x + +decodeModified :: [ListItem a] -> [a] +decodeModified = concatMap fromListItem diff --git a/Problems 11-20/problem_13.hs b/Problems 11-20/problem_13.hs new file mode 100644 index 0000000..303dfbd --- /dev/null +++ b/Problems 11-20/problem_13.hs @@ -0,0 +1,21 @@ +-- This is the same solution as in Problem 11 +data ListItem a = Single a | Multiple Int a deriving (Show) + +getListItem :: (Int, a) -> ListItem a +getListItem (1, x) = Single x +getListItem (n, x) = Multiple n x + +encode :: (Eq a) => [a] -> [(Int, a)] +encode = + foldr + ( \x acc -> case acc of + [] -> [(1, x)] + (hd@(hd_n, hd_a) : tl) -> + if x == hd_a + then (hd_n + 1, hd_a) : tl + else (1, x) : hd : tl + ) + [] + +encodeModified :: (Eq a) => [a] -> [ListItem a] +encodeModified = map getListItem <$> encode diff --git a/Problems 11-20/problem_14.hs b/Problems 11-20/problem_14.hs new file mode 100644 index 0000000..07daec8 --- /dev/null +++ b/Problems 11-20/problem_14.hs @@ -0,0 +1,2 @@ +dupli :: [a] -> [a] +dupli x = concat $ [replicate 2] <*> x diff --git a/Problems 11-20/problem_15.hs b/Problems 11-20/problem_15.hs new file mode 100644 index 0000000..1040685 --- /dev/null +++ b/Problems 11-20/problem_15.hs @@ -0,0 +1,2 @@ +repli :: [a] -> Int -> [a] +repli x n = concat $ [replicate n] <*> x diff --git a/Problems 11-20/problem_16.hs b/Problems 11-20/problem_16.hs new file mode 100644 index 0000000..0d7d1c8 --- /dev/null +++ b/Problems 11-20/problem_16.hs @@ -0,0 +1,2 @@ +dropEvery :: [a] -> Int -> [a] +dropEvery l n = map snd . filter ((0 /=) . (`mod` n) . fst) $ zip [1 ..] l diff --git a/Problems 11-20/problem_17.hs b/Problems 11-20/problem_17.hs new file mode 100644 index 0000000..0d8ee13 --- /dev/null +++ b/Problems 11-20/problem_17.hs @@ -0,0 +1,5 @@ +split :: [a] -> Int -> ([a], [a]) +split x 0 = ([], x) +split (hd : tl) n = (hd : next_hd, next_tl) + where + (next_hd, next_tl) = split tl (n - 1) diff --git a/Problems 11-20/problem_18.hs b/Problems 11-20/problem_18.hs new file mode 100644 index 0000000..04fcf3c --- /dev/null +++ b/Problems 11-20/problem_18.hs @@ -0,0 +1,4 @@ +slice :: [a] -> Int -> Int -> [a] +slice [] _ _ = error "Invalid indices" +slice l 1 n = take n l +slice (_ : tl) m n = slice tl (m - 1) (n - 1) diff --git a/Problems 11-20/problem_19.hs b/Problems 11-20/problem_19.hs new file mode 100644 index 0000000..442521c --- /dev/null +++ b/Problems 11-20/problem_19.hs @@ -0,0 +1,5 @@ +rotate :: [a] -> Int -> [a] +rotate l n = + if n >= 0 + then (\(x, y) -> y ++ x) $ splitAt n l + else rotate l (length l + n) diff --git a/Problems 11-20/problem_20.hs b/Problems 11-20/problem_20.hs new file mode 100644 index 0000000..628e5a2 --- /dev/null +++ b/Problems 11-20/problem_20.hs @@ -0,0 +1,5 @@ +removeAt :: Int -> [a] -> (a, [a]) +removeAt n ls = (last l, take m l ++ r) + where + (l, r) = splitAt n ls + m = length l - 1