new: Solved problems 11 to 20
This commit is contained in:
parent
861ab7da03
commit
a6e4866f22
10 changed files with 74 additions and 0 deletions
20
Problems 11-20/problem_11.hs
Normal file
20
Problems 11-20/problem_11.hs
Normal file
|
@ -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
|
8
Problems 11-20/problem_12.hs
Normal file
8
Problems 11-20/problem_12.hs
Normal file
|
@ -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
|
21
Problems 11-20/problem_13.hs
Normal file
21
Problems 11-20/problem_13.hs
Normal file
|
@ -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
|
2
Problems 11-20/problem_14.hs
Normal file
2
Problems 11-20/problem_14.hs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
dupli :: [a] -> [a]
|
||||||
|
dupli x = concat $ [replicate 2] <*> x
|
2
Problems 11-20/problem_15.hs
Normal file
2
Problems 11-20/problem_15.hs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
repli :: [a] -> Int -> [a]
|
||||||
|
repli x n = concat $ [replicate n] <*> x
|
2
Problems 11-20/problem_16.hs
Normal file
2
Problems 11-20/problem_16.hs
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
dropEvery :: [a] -> Int -> [a]
|
||||||
|
dropEvery l n = map snd . filter ((0 /=) . (`mod` n) . fst) $ zip [1 ..] l
|
5
Problems 11-20/problem_17.hs
Normal file
5
Problems 11-20/problem_17.hs
Normal file
|
@ -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)
|
4
Problems 11-20/problem_18.hs
Normal file
4
Problems 11-20/problem_18.hs
Normal file
|
@ -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)
|
5
Problems 11-20/problem_19.hs
Normal file
5
Problems 11-20/problem_19.hs
Normal file
|
@ -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)
|
5
Problems 11-20/problem_20.hs
Normal file
5
Problems 11-20/problem_20.hs
Normal file
|
@ -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
|
Loading…
Reference in a new issue