-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_length_encoder.rb
executable file
·49 lines (41 loc) · 1.07 KB
/
run_length_encoder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env ruby
class RunLengthEncoder
def initialize(string)
@data = string
end
def encode
result = ''
count = 0
last_char = nil
@data.split(//).each do |char|
last_char ||= char
if last_char == char
count += 1
else
result << "#{count}_#{last_char}"
last_char = char
count = 1
end
end
result << "#{count}_#{last_char}"
result
end
def decode
result = ''
@data.scan(/(\d+_)(\w{1}|\s)/).each do |count, character|
result << character * count.gsub("_", "").to_i
end
result
end
end
# run_length_encoder.rb -e 'zzzzzzzzzzzzzzzzzzzzz wwwwwwwwwwwaaabbbbbbbbcdeeeeeeeeeeefff11111222333'
if ARGV[0] == '-e'
encoded = RunLengthEncoder.new(ARGV[1]).encode
puts ("encoded sequence = #{encoded}")
puts "compression ratio #{(1 - encoded.length / ARGV[1].length.to_f) * 100}%"
end
# run_length_encoder.rb -d 6_a5_b4_c
if ARGV[0] == '-d'
decoded = RunLengthEncoder.new(ARGV[1]).decode
puts ("decoded sequence = #{decoded}")
end