Skip to content

Instantly share code, notes, and snippets.

@abo-elleef
Last active July 15, 2020 22:11
Show Gist options
  • Save abo-elleef/f628df2c1e1a1875c8a1034d0791dd84 to your computer and use it in GitHub Desktop.
Save abo-elleef/f628df2c1e1a1875c8a1034d0791dd84 to your computer and use it in GitHub Desktop.
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