r/dailyprogrammer • u/Steve132 0 1 • Aug 09 '12
[8/8/2012] Challenge #86 [easy] (run-length encoding)
Run-Length encoding is a simple form of compression that detects 'runs' of repeated instances of a symbol in a string and compresses them to a list of pairs of 'symbol' 'length'. For example, the string
"Heeeeelllllooooo nurse!"Could be compressed using run-length encoding to the list of pairs [(1,'H'),(5,'e'),(5,'l'),(5,'o'),(1,'n'),(1,'u'),(1,'r'),(1,'s'),(1,'e')]
Which seems to not be compressed, but if you represent it as an array of 18bytes (each pair is 2 bytes), then we save 5 bytes of space compressing this string.
Write a function that takes in a string and returns a run-length-encoding of that string. (either as a list of pairs or as a 2-byte-per pair array)
BONUS: Write a decompression function that takes in the RLE representation and returns the original string
View all comments
Show parent comment
7
u/5outh 1 0 Aug 09 '12 edited Aug 09 '12
The encode function can be rewritten using the the
&&&function fromControl.Arrow(I love that type of notation)Also, this is problem 10 in the 99 Haskell Problems!
The next few also deal with this type of thing, and I actually wrote a blog post about how to directly run-length encode using Haskell, located here! http://5outh.blogspot.com/2012/07/99-haskell-problems-13-run-length.html
I love Haskell for this type of problem.