-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_demystifier.rb
41 lines (32 loc) · 1.11 KB
/
string_demystifier.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
class StringDemystifier
def initialize
@string = "!YTIRCO!IQIIQIDEMGMMIM FO YMJMMSM!RA !EGEEJEHT ROEOOSOF PAEJEEBEL TN!AIKIITIG ENVNNMNO ,GQGGCGN!ILEKIZIISIRT A RJRRDROF PETOTTJTS LLZLLEL!AMSXSSMS ENODOOSO"
end
def demystify
demystify_process1
demystify_process2
demystify_process3
@string
end
def demystify_process1
#Replace sub str like sos, oso...... to sss or ooo
("A".."Z").to_a.each do |c|
@string.gsub!(Regexp.new("#{c}[A-Z]{1}#{c}"), c*3)
end
end
def demystify_process2
#Replace sub str like ssssss, oooooo...... to s or o
("A".."Z").to_a.each do |c|
@string.gsub!(Regexp.new(c*6), c)
end
#We can also use below for this case, but may be possible it's not working with random strings
#@string.squeeze!
end
def demystify_process3
#Remove ! and reverse str
@string = @string.split(" ").collect{|s| s.reverse }.reverse.join(" ").gsub("!", "")
end
end
demystifier_string = StringDemystifier.new
puts demystifier_string.demystify
#Output : ONE SMALL STEP FOR A TRIKELING, ONE GIANT LEAP FOR THE ARMY OF MEDIOCRITY