Advertisement
banovski

Ninety-Nine Haskell Problems: #10

Apr 10th, 2025 (edited)
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 2.46 KB | Source Code | 0 0
  1. -- Implement the so-called run-length encoding data compression
  2. -- method. Consecutive duplicates of elements are encoded as lists (N
  3. -- E) where N is the number of duplicates of the element E:
  4. -- "aaaabccaadeeee" ->
  5. -- [(4,'a'),(1,'b'),(2,'c'),(2,'a'),(1,'d'),(4,'e')]
  6.  
  7. import Data.List
  8.  
  9. main :: IO ()
  10. main = do
  11.   putStrLn "Validity test results: "
  12.   mapM_ (print . functionTester) testFunctions
  13.  
  14. testFunctions :: Eq a => [[a] -> [(Int, a)]]
  15. testFunctions = [one
  16.   , two
  17.   , three
  18.   , four
  19.   , five]
  20.  
  21. testList :: [String]
  22. testList = ["a", "aa", "ab", "aaa", "aab", "aba", "baa", "aaaa","aaab","aaba","aabb", "abbb"]
  23.  
  24. verificationList :: [[(Int, Char)]]
  25. verificationList = [
  26.     [(1,'a')]
  27.   , [(2,'a')]
  28.   , [(1,'a'),(1,'b')]
  29.   , [(3,'a')]
  30.   , [(2,'a'),(1,'b')]
  31.   , [(1,'a'),(1,'b'),(1,'a')]
  32.   , [(1,'b'),(2,'a')]
  33.   , [(4,'a')]
  34.   , [(3,'a'),(1,'b')]
  35.   , [(2,'a'),(1,'b'),(1,'a')]
  36.   , [(2,'a'),(2,'b')]
  37.   , [(1,'a'),(3,'b')]]
  38.  
  39. functionTester :: (String -> [(Int, Char)]) -> Bool
  40. functionTester function = verificationList == map function testList
  41.  
  42. -- Tested functions. Latency is measured in ticks. The size of the
  43. -- test list for functionns one to four is 1000405 items, for the
  44. -- fifth function, due to its ineffiency, its 10011 itmes.
  45.  
  46. -- 144
  47. one :: Eq a => [a] -> [(Int, a)]
  48. one = encode . group
  49.   where
  50.     encode = map (\x -> (length x, head x))
  51.  
  52. -- 163
  53. two :: Eq a => [a] -> [(Int, a)]
  54. two [] = []
  55. two lst = foldr go [(1, last lst)] (init lst)
  56.   where
  57.     go i ((n, v):xs)
  58.         | i == v = (n + 1, v) : xs
  59.         | otherwise = (1, i) : (n, v) : xs
  60.  
  61. -- 155
  62. three :: Eq a => [a] -> [(Int, a)]
  63. three [] = []
  64. three lst = reverse $ foldl go [(1, head lst)] (tail lst)
  65.   where
  66.     go ((n, v):xs) i
  67.       | v == i = (n + 1, v) : xs
  68.       | otherwise = (1, i) : (n, v) : xs
  69.  
  70. -- 79
  71. four :: Eq a => [a] -> [(Int, a)]
  72. four [] = []
  73. four list = reverse $ go [(1, head list)] (tail list)
  74.   where
  75.     go acc [] = acc
  76.     go ((n, v):xs) (y:ys)
  77.       | v == y = go ((n + 1, v) : xs) ys
  78.       | otherwise = go ((1, y) : (n, v) : xs) ys
  79.  
  80. -- 90 ticks; tested on a list with 10011 items.
  81. five :: Eq a => [a] -> [(Int, a)]
  82. five [] = []
  83. five list = go [(1, head list)] (tail list)
  84.   where
  85.     go acc [] = acc
  86.     go acc (y:ys)
  87.       | (snd . last) acc == y =
  88.         go (init acc ++ [(fst (last acc) + 1, snd (last acc))]) ys
  89.       | otherwise = go (acc ++ [(1, y)]) ys
  90.  
  91. -- Validity check results:
  92. -- True
  93. -- True
  94. -- True
  95. -- True
  96. -- True
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement