Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2018 17:01
Show Gist options
  • Save anonymous/b533d29afb9ce1d543e93efc7dc1a68f to your computer and use it in GitHub Desktop.
Save anonymous/b533d29afb9ce1d543e93efc7dc1a68f to your computer and use it in GitHub Desktop.
input = ["A/B/C/", "A/B/D/", "A/B/E/", "A/F/G/"]
mut = {}
def f(hash, str)
left, rest = str.split("/", 2)
return if left.empty?
hash[left] ||= {}
f(hash[left], rest) unless rest.empty?
end
input.each { |str| f(mut, str) }
puts mut.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment