# Bracket Packing (#78) (2 solutions)

**URL:** <https://www.ruby-forum.com/t/bracket-packing-78-2-solutions/59863>\
**Category:** Ruby\
**Created:** [May 7, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/bracket-packing-78-2-solutions/59863 "2006-05-07T20:38:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@Eric\_I](https://www.ruby-forum.com/u/Eric_I)\
**Post date:** [May 7, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/bracket-packing-78-2-solutions/59863/1 "2006-05-07T20:38:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Eric\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@Eric\_I](https://www.ruby-forum.com/u/Eric_I)\
**Post date:** [May 7, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/bracket-packing-78-2-solutions/59863/2 "2006-05-07T23:28:33Z")

</div>

On May 7, 2006, at 1:36 PM, Eric I. wrote:

> [0].count(symbol\_pair) % 2 }  
> puts ARGV[0]

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

“)B(” or “B()”

?

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Eric\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@Eric\_I](https://www.ruby-forum.com/u/Eric_I)\
**Post date:** [May 11, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/bracket-packing-78-2-solutions/59863/3 "2006-05-11T19:16:00Z")

</div>

Huhu James,  
I think “)B(” or “B()” are not valid outputs, because the program only  
misses off brackets.

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

Please tell me if I misinterpreted the machine.

Kind regards.  
Robert

James Edward G. II schrieb:

---

<div class="post-metadata">

**Author:** ![Eric\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@Eric\_I](https://www.ruby-forum.com/u/Eric_I)\
**Post date:** [May 11, 2006, 7:25pm UTC](https://www.ruby-forum.com/t/bracket-packing-78-2-solutions/59863/4 "2006-05-11T19:25:22Z")

</div>

On May 11, 2006, at 12:13 PM, Robert R. wrote:

> Huhu James,  
> I think “)B(” or “B()” are not valid outputs, because the program only  
> misses off brackets.
> 
> “Now, the problem is that this venerable program has for some reason  
> begun to output malformed packaging descriptions, occasionally missing  
> off a bracket”
> 
> Please tell me if I misinterpreted the machine.

Right. I think that’s pretty much what everyone agreed on.

James Edward G. II
