[QUIZ] Bracket Packing (#78)

I don't think an outer wrapper should be required, and both BB and
(B)(B) are technically legal, though BB is another one of those that
wouldn't occur in correct input, for the reason Logan mentioned.

In my (increasingly detailed :)) mental image of this packing factory,
the packer automatically adds a shipping outer around each set of packed
brackets anyway - what we're packing here are the display containers.

···

On Sun, 2006-05-07 at 10:35 +0900, Gautam Dey wrote:

On May 6, 2006, at 4:20 PM, Logan Capaldo wrote:

>
> On May 6, 2006, at 6:46 PM, Gautam Dey wrote:
>>>
>>> Do the brackets have to have a parent wrapper?
>>>
>>> i.e. {(B)(B)} is of course valid
>>> but (B)(B) isn't.
>>>
>>> I imagine that's the case, I just want to make sure.
>>>
>>> Pat
>>>
>>
>> Also do they have to have a wrapper at all? I assumed they did not
>> and that they did not need to have a parent wrapper.
>>
>> I assumed that BB and (B)(B) were both legal.
>>
>> Gautam.
>>
>
> Why are you even sending them to the packer then, if they aren't
> going to be packed?
>

This is true. I think I made the assumption as a simplification. When
I was thinking about the problem.

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk

Here is my solution,

   Though, it adds the outer packaging if one is not there. And if it can not fix the string fails with an exit code of 1. Since, I'm a noob to ruby it is not pretty.

bracketstest.rb (3.65 KB)

Gautam Dey <gdey@mac.com> writes:

Here is my solution,

and this is mine, actually quite different from the ones I saw; it
uses gsubs to reduce step-by-step.

def unwrap(desc)
  [desc.gsub!('BB', 'B'), desc.gsub!('(B)', 'B'),
   desc.gsub!('[B]', 'B'), desc.gsub!('{B}', 'B')].nitems > 0
end

def valid?(desc)
  desc = desc.dup
  true while unwrap desc
  desc == "B"
end

packet = ARGV.first.to_s
if valid? packet
  puts packet
  exit 0
else
  exit 1
end

···

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org