-
Notifications
You must be signed in to change notification settings - Fork 252
/
run_length_encoding.rb
65 lines (59 loc) · 1.54 KB
/
run_length_encoding.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Problem: https://exercism.org/tracks/ruby/exercises/run-length-encoding
# Solution (Brute force)
class RunLengthEncoding
def self.encode(input)
return "" if input==""
encoded_msg = ""
curr_char_count = 0
prev_char = ""
input.chars.each do |char|
if prev_char != char
encoded_msg+="#{curr_char_count>1?curr_char_count:""}#{prev_char}"
prev_char = char
curr_char_count=0
end
curr_char_count+=1
end
encoded_msg+="#{curr_char_count>1?curr_char_count:""}#{prev_char}"
encoded_msg
end
def self.decode(encoded_input)
decoded_msg = ""
curr_num_str = ""
curr_char = ""
encoded_input.chars.each do |char|
if char.match(/\d/)
curr_num_str+=char
next
end
decoded_msg+="#{curr_num_str=="" ? char: (char*curr_num_str.to_i)}"
curr_num_str = ""
end
decoded_msg
end
end
# Solution(Using Regex)
class RunLengthEncoding
def self.encode(input)
input.gsub(/(.)\1+/) { |m| "#{m.length}#{m[0]}" }
end
def self.decode(input)
input.gsub(/\d+./) { |m| m[-1] * m.to_i }
end
end
# Solution(Using Collections)
class RunLengthEncoding
def self.encode(input)
input
.chars
.chunk { |c| c }
.collect { |k,v| [v.length > 1 ? v.length : '', k].join }
.join
end
def self.decode(input)
input
.scan(/(\d*)([ \w]+?)/)
.map { |c| c[1] * (c[0] != '' ? c[0].to_i : 1) }
.join
end
end