Skip to content

Instantly share code, notes, and snippets.

@ta1kt0me
Created July 31, 2020 13:11
Show Gist options
  • Save ta1kt0me/45de1c69ebad5209b65ccc1519aca791 to your computer and use it in GitHub Desktop.
Save ta1kt0me/45de1c69ebad5209b65ccc1519aca791 to your computer and use it in GitHub Desktop.
Run Length 圧縮
require 'minitest/autorun'
class CompressString
def initialize(text)
@text = text
end
def text
@text
end
def compress
compressed = []
chars = text.chars
current = chars.first
count = 0
chars.each.with_index do |char, i|
if current == char
count += 1
else
current = char
count = 1
end
compressed << [current, count] if char != chars[i+1]
end
compressed.flatten.join
end
end
class CompressStringText < Minitest::Test
def test_compress
input = 'RRRRRRABDDDDDGG'
expected = 'R6A1B1D5G2'
cs = CompressString.new(input)
assert_equal expected, cs.compress
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment