Created
December 2, 2021 11:06
-
-
Save Niall47/6238154ce6732f68fd4100d76607e636 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Run length encodingRun-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count. | |
For example we can represent the original 53 characters with only 13. | |
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB" | |
RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression."AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE" | |
For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace. | |
This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character. | |
=end | |
# Usage example | |
# ruby rle.rb encode WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB | |
# ruby rle.rb decode 12WB12W3B24WB | |
class RLE | |
def encode(value) | |
# (.) grab everything | |
# \1+ that repeats more than once | |
# and substitute it for the string length and the first character | |
puts value.gsub(/(.)\1+/).each { |character| "#{character.length}#{character[0]}" } | |
end | |
def decode(value) | |
# \d grab each number | |
# +. and the following character | |
# substitute it for the last character returned by the regex multiplied by the integer that comes before it | |
puts value.gsub(/\d+./).each { |characters| "#{characters[-1] * characters.chop.to_i}"} | |
end | |
end | |
throw 'wrong number of arguments' if ARGV.count != 2 | |
RLE.new.send(ARGV.first, ARGV.last) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment