Last active
July 15, 2020 22:11
-
-
Save abo-elleef/f628df2c1e1a1875c8a1034d0791dd84 to your computer and use it in GitHub Desktop.
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
def resolve(string) | |
string.gsub(/\d\[(.*?)\]/){|a| | |
formated = a.split("[")[1..-1].join("[") | |
if formated.include?('[') | |
resolve(formated) * a.split("[").first.to_i | |
else | |
a.split("[").last * a.split("[").first.to_i | |
end | |
}.gsub("]", "") | |
end | |
input1 = "4[ab]" | |
input2 = "2[b3[a]]" | |
input3 = "z1[y]zzz2[abc]" | |
raise "oh oh missing this input #{input1} with the following result #{resolve(input1)}" unless resolve(input1) == 'abababab' | |
raise "oh oh missing this input #{input2} with the following result #{resolve(input2)}" unless resolve(input2) == 'baaabaaa' | |
raise "oh oh missing this input #{input3} with the following result #{resolve(input3)}" unless resolve(input3) == 'zyzzzabcabc' | |
p "all tests pass Yah :D " | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment