Sha256: c9b4dd290f24e30ff3e74b126dafd061e746934ad8e960b6d0b6f2d7ae4e64fa
Contents?: true
Size: 671 Bytes
Versions: 239
Compression:
Stored size: 671 Bytes
Contents
module BookKeeping VERSION = 3 end class RunLengthEncoding def self.encode(str) str.chars.chunk { |char| char }.each_with_object('') do |chunk, out| out << encoded(chunk) end end def self.decode(str) str.scan(/(\d+)?(\D)/).each_with_object('') do |captures, out| out << decoded(captures) end end # private def self.encoded(chunk) char = chunk.first times = chunk.last.count return char if times == 1 "#{times}#{char}" end private_class_method :encoded def self.decoded(captures) times = (captures.first || 1).to_i char = captures.last char * times end private_class_method :decoded end
Version data entries
239 entries across 239 versions & 1 rubygems