Created
July 31, 2020 13:11
-
-
Save ta1kt0me/45de1c69ebad5209b65ccc1519aca791 to your computer and use it in GitHub Desktop.
Run Length 圧縮
This file contains 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
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