I found a problem statement over the internet, was trying to solve it in ruby.
Input and output are self explanatory.
Eg. 1, Input : 2[abc]3[ab]c
Output : abcabcabcababababc
Explanation : 2
times abc
and 3
times ab
and then c
Eg. 2
Input : 2[3[a]b]
Output : aaabaaab
I tried this,
But it doesn't work for multiple level.
num = 1
str = ""
"3[abc]4[ab]2[3[a]b]c".each_char do |ch|
if ch.to_i != 0
num = ch
elsif ch == "["
next
elsif ch == "]"
num.to_i.times { print str }
str = ""
num = 1
else
str << ch
end
end
I was looking for recursive call.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…