RosettaCodeData/Task/Run-length-encoding/Ruby/run-length-encoding-1.rb

15 lines
325 B
Ruby
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
# run_encode("aaabbbbc") #=> [["a", 3], ["b", 4], ["c", 1]]
def run_encode(string)
string
.chars
.chunk{|i| i}
.map {|kind, array| [kind, array.length]}
2013-04-10 23:57:08 -07:00
end
2015-11-18 06:14:39 +00:00
# run_decode([["a", 3], ["b", 4], ["c", 1]]) #=> "aaabbbbc"
def run_decode(char_counts)
char_counts
.map{|char, count| char * count}
.join
2013-04-10 23:57:08 -07:00
end