6 kyu

Run-length encoding

298 of 9,845bkaes

Description:

Run-length encoding (RLE) is a very simple form of data compression in which runs of data (that is, sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run. Wikipedia

Task

Your task is to write such a run-length encoding. For a given string, return a list (or array) of pairs (or arrays) [ (i1, s1), (i2, s2), …, (in, sn) ], such that one can reconstruct the original string by replicating the character sx ix times and concatening all those strings. Your run-length encoding should be minimal, ie. for all i the values si and si+1 should differ.

Examples

As the article states, RLE is a very simple form of data compression. It's only suitable for runs of data, as one can see in the following example:

runLengthEncoding "hello world!" 
        `shouldBe` [(1,'h'), (1,'e'), (2,'l'), (1,'o'), (1,' '), (1,'w'),(1,'o'), (1,'r'), (1,'l'), (1,'d'), (1,'!')]
runLengthEncoding "hello world!"
 # =>      [[1,'h'], [1,'e'], [2,'l'], [1,'o'], [1,' '], [1,'w'], [1,'o'], [1,'r'], [1,'l'], [1,'d'], [1,'!']]
runLengthEncoding("hello world!")
 //=>      [[1,'h'], [1,'e'], [2,'l'], [1,'o'], [1,' '], [1,'w'], [1,'o'], [1,'r'], [1,'l'], [1,'d'], [1,'!']]
run_length_encoding("hello world!")
 //=>      [[1,'h'], [1,'e'], [2,'l'], [1,'o'], [1,' '], [1,'w'], [1,'o'], [1,'r'], [1,'l'], [1,'d'], [1,'!']]
rle("hello world!")
 # => [[1,'h'], [1,'e'], [2,'l'], [1,'o'], [1,' '], [1,'w'], [1,'o'], [1,'r'], [1,'l'], [1,'d'], [1,'!']]
run_length_encoding("hello world!")
# => [[1,"h"], [1,"e"], [2,"l"], [1,"o"], [1," "], [1,"w"], [1,"o"], [1,"r"], [1,"l"], [1,"d"], [1,"!"]]

It's very effective if the same data value occurs in many consecutive data elements:

runLengthEncoding "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb" 
        `shouldBe` [(34,'a'), (3,'b')]
runLengthEncoding "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb" 
 #  => [[34,'a'], [3,'b']]
runLengthEncoding("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb")
 // => [[34,'a'], [3,'b']]
run_length_encoding("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb")
# => [[34,'a'], [3,'b']]
rle("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb")
# => [[34,'a'], [3,'b']]
run_length_encoding("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb")
# => [[34,"a"], [3,"b"]]
Strings
Algorithms

Stats:

CreatedNov 20, 2014
PublishedNov 20, 2014
Warriors Trained12950
Total Skips314
Total Code Submissions22136
Total Times Completed9845
Haskell Completions298
JavaScript Completions7789
CoffeeScript Completions36
Python Completions1580
Ruby Completions139
C Completions46
COBOL Completions7
Rust Completions59
Julia Completions9
Elixir Completions26
Total Stars115
% of votes with a positive feedback rating94% of 646
Total "Very Satisfied" Votes584
Total "Somewhat Satisfied" Votes50
Total "Not Satisfied" Votes12
Ad
Contributors
  • bkaes Avatar
  • jhoffner Avatar
  • laoris Avatar
  • ric Avatar
  • anter69 Avatar
  • docgunthrop Avatar
  • Voile Avatar
  • stellartux Avatar
  • trashy_incel Avatar
  • akar-0 Avatar
  • dfhwze Avatar
  • saudiGuy Avatar
Ad