Here is my solution to the first option of the quiz, which is simply
to detect if there is a problem. It makes use of the allowed
assumption that only one packaging character would be omitted in
incorrect input. So the underlying algorithm is to make sure there
are an even number of combined "("s and ")"s (and the same goes for
the other two types of packaging). The String#count method does much
of the heavy lifting.
["()", "{}", "[]"].each { |symbol_pair| exit(1) if 0 != ARGV[0].count
(symbol_pair) % 2 }
puts ARGV[0]
Here's another solution which repeatedly substitutes any of "(B)",
"[B]", "{B}", or "BB" with "B". In other words it just unwraps and
combines brackets. When all is said an done, one bracket should be
left if the wrapping description was correct.
description = ARGV[0].dup
while description.gsub!(/\(B\)|\[B\]|{B}|BB/, "B")
#empty
end
exit(1) unless description == "B"
puts ARGV[0]
Eric