[QUIZ] Bracket Packing (#78)

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

- Stu

···

-----Original Message-----
From: Ruby Quiz [mailto:james@grayproductions.net]
Sent: 05 May 2006 12:47
To: ruby-talk ML
Subject: [QUIZ] Bracket Packing (#78)

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz
until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby Talk follow the discussion. Please reply to the original quiz
message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-=-=-=-=

by Ross Bamford

The BigCo Bracket Company, one of the world's largest suppliers of
brackets,
hinges and fittings, has lately been experiencing problems in it's
manufacturing
division, with a large number or brackets lost or broken in transit
owing to
faulty packaging at the end of the line.

Investigations into the cause of the problem have led engineers to an
ancient
COBOL program controlling the packaging machinery. This program is
responsible
for selecting the type of packaging a given bracket should be shipped
in, based
on input from an array of sensors on the production line. It then sends
a
description of the package to the packager itself, which packs the
bracket and
sends it on to shipping. The description is a simple text string, made
up of
brackets with the following format:

  (B) - Bracket in a soft wrapping
  [B] - Bracket in a cardboard box
  {B} - Bracket in a wooden box

Often, brackets have multiple layers of packaging for protection, for
example:

  {(B)} - Soft-wrapped bracket in a wooden box
  [{B}] - Wooden-boxed bracket with cardboard outer
  
  [{(B)}{(B)(B)}] - Wooden boxed single and double bracket packs
with soft
                    inner wrap, in cardboard outer.

Now, the problem is that this venerable program has for some reason
begun to
output malformed packaging descriptions, occasionally missing off a
bracket, for
example:

  [{(B}{(B)(B)}]

or:

  {(B)}{(B)(B)}]

After a fruitless search for someone with the skills to fix this
problem, the
engineers changed tack and called you in to fix the problem from the
outside.

  What needs to be done?
  ======================

Basically, the plan is to insert another computer between the controller
and the
packer, upon which will run your program. The engineers can handle the
integration with their systems - they just need you to write a slick bit
of Ruby
code to validate the packaging descriptions as they're passed in. You've
been
given two choices:

  * Check the description, and return exitcode indicating it's
okay (0)
    or bad (1). If correct, you should also print the description
to stdout.
    If it's bad, the system can then force the controller to try
again.
  
  * Fix the description, if possible, and print it to stdout. The
system
    will then pass the fixed code to the packer.

<body>

<blockquote>
<font FACE="Arial,Arial" SIZE="1"><p ALIGN="JUSTIFY">This Email may contain confidential and privileged information and is intended for the use of the addressee(s) only. If you are not the intended recipient please notify the sender and delete the Email from your system. It should not be transmitted to any other person without the consent of the sender. Additional important notifications regarding Email transmissions from and to members of Baring Asset Management can be accessed at <a href="http://www.barings.com/email/index.hcst&quot;&gt;http://www.barings.com/email/index.hcst&lt;/a&gt;&lt;/font&gt;&lt;/p&gt;&lt;/body&gt;
</p>
</font>
</blockquote>

<p>&nbsp;</p>
</body>
</html>

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.

···

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

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

Surely that isn't possible, some combinations could have more than one possible
correction, eg:

[{BB] => [{B}B]
or
[{BB] => [{BB}]
or even,
[{BB] => [{}BB]
or even, (tho presumably this is invalid - some sort of black hole packaging):
[{BB]}

Perhaps the program should present the possible options and ask which is wanted?

···

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.

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

Surely that isn't possible, some combinations could have more than one possible
correction, eg:

[{BB] => [{B}B]
or
[{BB] => [{BB}]

Either of the above is valid - the main object of this is just to make
sure the packer doesn't get in a mess, and both of those will pass
through fine (even if the wrappings aren't exactly as intended).

or even,
[{BB] => [{}BB]
or even, (tho presumably this is invalid - some sort of black hole packaging):
[{BB]}

Both of these are invalid, and definitely not what was intended - there
won't be any empty packages (as in the first case) and the second isn't
balanced.

Perhaps the program should present the possible options and ask which is wanted?

If you want to output the options and fail with exitcode 1 for the
ambiguous ones, that's cool too.

···

On Fri, 2006-05-05 at 23:23 +0900, st103@doc.ic.ac.uk wrote:

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

Hmm, to clarify, brackets can have non-bracket siblings?

-mental

···

On Fri, 5 May 2006 23:38:05 +0900, Ross Bamford <rossrt@roscopeco.co.uk> wrote:

On Fri, 2006-05-05 at 23:23 +0900, st103@doc.ic.ac.uk wrote:

Surely that isn't possible, some combinations could have more than one

possible

correction, eg:

[{BB] => [{B}B]
or
[{BB] => [{BB}]

Either of the above is valid - the main object of this is just to make
sure the packer doesn't get in a mess, and both of those will pass
through fine (even if the wrappings aren't exactly as intended).

It's technically allowed in the output, but would never occur in a
correct input.

···

On Sat, 2006-05-06 at 04:42 +0900, MenTaLguY wrote:

On Fri, 5 May 2006 23:38:05 +0900, Ross Bamford <rossrt@roscopeco.co.uk> wrote:
> On Fri, 2006-05-05 at 23:23 +0900, st103@doc.ic.ac.uk wrote:
>> Surely that isn't possible, some combinations could have more than one
> possible
>> correction, eg:
>>
>> [{BB] => [{B}B]
>> or
>> [{BB] => [{BB}]
>
> Either of the above is valid - the main object of this is just to make
> sure the packer doesn't get in a mess, and both of those will pass
> through fine (even if the wrappings aren't exactly as intended).

Hmm, to clarify, brackets can have non-bracket siblings?

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