How to compress and decompress repeated string characters by storing the repeated length (RLE) in Ruby

1 Answer

0 votes
# ---------------------------------------------------------
#   Compress: turn "aaabbcccc" into "a3b2c4"
#   This is run-length encoding (RLE)
# ---------------------------------------------------------
def compress(s)
  return "" if s.empty?   # Edge case: empty string

  out = []                # Result string (array for efficiency)
  count = 1               # Count of repeated characters

  # Start from index 1 and compare with previous charactera
  (1..s.length).each do |i|

    # If still repeating the same character, increase count
    if i < s.length && s[i] == s[i - 1]
      count += 1
    else
      # Character changed OR reached end of string
      out << s[i - 1]     # Add the character
      out << count.to_s   # Add how many times it repeated
      count = 1           # Reset counter
    end
  end

  out.join
end


# ---------------------------------------------------------
#   Decompress: turn "a3b2c4" into "aaabbcccc"
#   Reads a letter, then reads digits, expands them.
# ---------------------------------------------------------
def decompress(s)
  out = []                # Result string
  current_char = nil      # The character being processed
  number = ""             # Digits representing the count

  s.each_char do |c|

    if c =~ /[A-Za-z]/    # Found a letter
      # If we already have a previous letter + number, expand it
      if current_char && !number.empty?
        out << current_char * number.to_i
      end

      current_char = c    # Start new character
      number = ""         # Reset number buffer

    elsif c =~ /\d/       # Found a digit
      number << c         # Build multi-digit number
    end
  end

  # Handle the last character + number pair
  if current_char && !number.empty?
    out << current_char * number.to_i
  end

  out.join
end


def main
  s = "wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww"

  c = compress(s)
  d = decompress(c)

  puts "Original:    #{s}"
  puts "Compressed:  #{c}"
  puts "Decompressed: #{d}"
end

main



=begin
run:

Original:    wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww
Compressed:  w12b1w10b3w6c4w12
Decompressed: wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww

=end

 



answered 1 day ago by avibootz

Related questions

...