Expands the curly-brace notation as supported by Bash.
expand "ab{c,d}e"
=> [ "abce", "abde" ]
expand "ab{c,d}e{1,2}"
=> [ "abce1", "abce2", "abde1", "abde2" ]
expand "ab{c,d{1,2}}e"
=> [ "abce", "abd1e", "abd2e" ]
This was a rudimentary attempt to see what could be written in < 20 minutes (for an interview-length question). There are certainly faster and more elegant solutions.
I've also added three additional solutions:
- A Ruby verion using regular expressions provided by @jakswa
- A recursive accumulator method in Ruby provided by @betawaffle
- The same recursive accumulator method in Erlang using tail recursion, also provided by @betawaffle
Note:
d_andy_expand
yields weird output when a}
is missing