[QUIZ] Bracket Packing (#78) (2 solutions)

James Edward Gray II writes:

> 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]

Just thinking out loud here, but how does this fair with inputs like:

")B(" or "B()"

?

You're correct, the code I provided could not deal with those situations. However, those should never be presented as inputs according to post 191513, which I'll quote:

Ross Bamford writes:

ยทยทยท

On May 7, 2006, at 1:36 PM, Eric I. wrote:
On Fri, 2006-05-05 at 22:52 +0900, Stuart Holden wrote:
> Quick question about the phrase 'occasionally missing off a bracket'. Is
> it possible to have lost more than one braket missing from the string,
> or will it be a case of 0..1 brackets missing? I assume just one. If it
> is possible to have more than one bracket missing, you could never trust
> what the program was saying, even when the string was valid.
>
> Ie, [{(B),(B)}] -> [(B),(B)]
>

Good point. Let's assume just one bracket will be missing, if any.
Obviously, to fix it, it needs to go back in the right place.

Eric