Hi,
I've refined my solution a little bit. It's now a bit smarter at
positioning missing opening brackets. The old version would misbehave
given...
[([B])}] -> [([B]){}]
The new version produces...
[([B])}] -> [{([B])}]
After spotting that we had a missing opening bracket, I was trying to
scan backwards to find the last unclosed occurrence of the next expected
bracket. This was getting a bit messy, then I spotted the technique used
by Ross to add the opening position to the stack along with the bracket
type - being a Ruby newbie I didn't think of pushing a tuple onto the
stack. Slotted that technique in there, and it seems to work a charm.
Some test results below...
[B] -> [B] (valid=true)
[[B] -> [[B]] (valid=false)
[B]] -> [[B]] (valid=false)
[(B)] -> [(B)] (valid=true)
[(B] -> [(B)] (valid=false)
[B)] -> [(B)] (valid=false)
[([B])}] -> [{([B])}] (valid=false)
[{([B])] -> [{([B])}] (valid=false)
[(B)(B)(B)}] -> [{(B)(B)(B)}] (valid=false)
[(B){(B)(B)({B})] -> [(B){(B)(B)({B})}] (valid=false)
[(B)(B)(B)({B}})] -> [(B)(B)(B)({{B}})] (valid=false)
[(B)(B)(B)}({B})] -> [{(B)(B)(B)}({B})] (valid=false)
Regards,
Stu
packer.rb (3.8 KB)
···
-----Original Message-----
From: Stuart Holden
Sent: 09 May 2006 00:22
To: ruby-talk ML
Subject: Re: [QUIZ] Bracket Packing (#78)
Here is my attempt. Go easy... my first quiz entry, and my first Ruby
script.
Regards,
Stu
<snipped/>
<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">http://www.barings.com/email/index.hcst</a></font></p></body>
</p>
</font>
</blockquote>
<p> </p>
</body>
</html>