Created
June 18, 2014 18:49
-
-
Save DeepSky8/9602c8bc83f6459b0688 to your computer and use it in GitHub Desktop.
P10 (*) Run-length encoding of a list. Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as tuples (N, E) where N is the number of duplicates of the element E. Example: scala> encode(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) res…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def encode[A](input: List[A]): List[(Int, A)] = input match { | |
| case Nil => Nil | |
| case h :: t => (pack(input)).map(shorten) | |
| } | |
| def shorten[A](input: List[A]): List[(Int, A)] = input match { | |
| case Nil => Nil | |
| case h :: t => (input.length, h) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment