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
6
u/[deleted] Aug 09 '12
Not perfect, but here we go, C++: http://ideone.com/t25IJ
17
u/Tekmo Aug 09 '12
Haskell:
Bonus:
4
u/daveasaurus Aug 09 '12 edited Aug 09 '12
Python:
Sample Input:
Note that the above includes spaces and exclamation marks (which the original post does not?)
Also I tried it using this reg-ex approach which is probably not as efficient as just going through each character in the string, but I wanted to try it this way :D
Other sample:
( I didn't include all the 2000+ exclamation marks above :)
BONUS:
Run code at ideone, Github gist
4
u/oskar_stephens Aug 10 '12 edited Aug 10 '12
Ruby:
I'm not entirely happy with the double match in the regex, any tips on how to match a variable number of the same characters in a match?
The nested array feels a bit weird too, this seems like the perfect place for tuples like in Python.
8
u/flowblok Aug 09 '12 edited Aug 09 '12
Python solution: itertools ftw!
3
u/joboscribe Aug 09 '12 edited Aug 09 '12
Mine is not as cool as daveasaurus because i'm not checking arg length, nor did i regex that puppy. So my hat goes off to you sir.
Python (with bonus):
edit: Output
[[1, 't'], [1, 'h'], [1, 'i'], [5, 's'], [1, ' '], [1, 'i'], [1, 's'], [1, ' '], [1, 'a'], [1, ' '], [5, 's'], [1, 'n'], [1, 'a'], [1, 'k'], [1, 'e'], [1, '!']]
'thisssss is a sssssnake!'
3
u/[deleted] Aug 09 '12
Java:
3
u/Wedamm Aug 09 '12
Haskell:
Test:
4
u/rya11111 3 1 Aug 09 '12
Please note that challenge #86 intermediate and difficult have been given here:
intermediate and difficult and upvote it so that other people can see it too .. we dont get any karma for self posts.
also we have a header which is updated for every new challenge posted in case you cannot see the posts for some reason.
2
u/mathiasbynens 0 0 Aug 09 '12
In your example, non-alphabetic characters are ignored. I decided not to do this.
JavaScript (with Node.js):
Example output:
2
u/andkerosine Aug 09 '12
All-in-one Ruby:
2
u/ae7c Aug 09 '12
Python
Bonus:
2
u/paininthebass Aug 09 '12
C++11:
2
u/goldjerrygold_cs Aug 09 '12
boring ol' Python
2
u/Puzzel Aug 09 '12
Python
2
u/sleepingsquirrel Aug 09 '12
Prolog:
...I'm unhappy about having separate
rleandunrlerelations, which are identical, except for the order of the clauses. I suppose a NuProlog programmer could fix that up. Also, I don't like the cut, and writing a recursive relation seems like a failure.2
u/mudkip1123 0 0 Aug 09 '12
Python:
print encode("Heeeeelllllooooo nurse!") yields
2
u/path411 Aug 10 '12
JavaScript
fiddle: http://jsfiddle.net/path411/7XqZZ/
2
u/bschlief 0 0 Aug 10 '12
Ruby, based upon oskar_stephen's regex because my own regex-fu is still a work in progress. Still puzzling through andkerosine's solution, too.
1
u/sleepingsquirrel Aug 09 '12
Perl:
1
u/ctdonath Aug 09 '12
Canonical C++:
1
u/compmstr Aug 09 '12
Clojure:
Bonus:
1
u/howeyc Aug 10 '12
Common Lisp:
Bonus:
1
u/devilsassassin Aug 10 '12
Challenge in Java with generics and the Bonus:
Second class:
1
u/SPxChairman 0 0 Aug 10 '12
Python:
1
u/[deleted] Aug 10 '12
C++:
1
u/cdelahousse Aug 10 '12
Javascript with bonus
1
u/brasil14 Aug 10 '12
Python with recursive bonus function:
1
u/thomdrew Aug 10 '12
Here's my crappy Java solution:
1
u/MichaelTheDevel Aug 11 '12
Here was my solution Python solution on Github
1
u/[deleted] Aug 11 '12
Python
1
u/pkraack Aug 13 '12
some c# u likez?
1
u/robin-gvx 0 2 Aug 13 '12
This was yesterday's, but ah well:
1
u/Ozera 0 0 Aug 14 '12
Python:
http://codepad.org/epdXlwSk
1
u/Lurker378 Aug 16 '12 edited Aug 16 '12
A clojure implementation:
Note this comes out backward, but it comes out as a map so you can do (get (encode "hello") \h) for the amount of h's, if you reverse it, it get's converted to a sequence of vectors and you lose this ability.
1
u/larg3-p3nis Aug 18 '12
Java. Compression and decompression functions.
1
u/mordisko Aug 22 '12
Python 3:
Bonus:
1
u/Erocs Aug 22 '12
Scala 2.9
1
u/CzechsMix 0 0 Sep 14 '12
Untested befunge, will posted fixed code if need be once I test it thouroughly....
1
u/marekkpie Jan 09 '13 edited Jan 16 '13
Lua. With bonus.
C. Had a bit of trouble with trying to use
strspn. Went with a bit of an ugly, brutish way:1
u/uhwuggawuh Feb 02 '13
Python:
1
u/Dr-GQ Dec 20 '23
I have a perl version that work for binary data too it encodes the "Heeeeelllllooooo nurse!" in 14Bytes
1
u/ipelkowski Aug 10 '12
wow i see now python is a non complex language, alot easier then c++ or java but i still dont get it and ive been trying