# Ruby Quiz Contest Winner!

**URL:** <https://www.ruby-forum.com/t/ruby-quiz-contest-winner/59937>\
**Category:** Ruby\
**Created:** [May 8, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/ruby-quiz-contest-winner/59937 "2006-05-08T21:02:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [May 8, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/ruby-quiz-contest-winner/59937/1 "2006-05-08T21:02:13Z")

</div>

Ross B. has submitted a quiz that exceeded the mark I said I  
would give a book to. Way to go Ross! (Thanks to everyone else who  
entered as well.)

Ross, if you will send me your address off-list, I’ll get a book on  
its way.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [May 8, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/ruby-quiz-contest-winner/59937/2 "2006-05-08T21:55:46Z")

</div>

On 5/8/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ross B. has submitted a quiz that exceeded the mark I said I  
> would give a book to.

Will his quiz be the next? You have defintely piqued my curiousity.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [May 8, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/ruby-quiz-contest-winner/59937/3 "2006-05-08T22:05:13Z")

</div>

On May 8, 2006, at 3:54 PM, Belorion wrote:

> On 5/8/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Ross B. has submitted a quiz that exceeded the mark I said I  
> > would give a book to.
> 
> Will his quiz be the next? You have defintely piqued my curiousity.

Um, for him to have won, his quiz would have had to have already occured

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [May 8, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/ruby-quiz-contest-winner/59937/4 "2006-05-08T22:11:57Z")

</div>

On May 8, 2006, at 3:03 PM, Logan C. wrote:

> > Will his quiz be the next? You have defintely piqued my curiousity.
> 
> Um, for him to have won, his quiz would have had to have already  
> occured

Ross B. wrote this week’s quiz (the winner)… and next week’s  
too. 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [May 8, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/ruby-quiz-contest-winner/59937/5 "2006-05-08T22:11:51Z")

</div>

On 5/8/06, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Um, for him to have won, his quiz would have had to have already occured

My apologies, I’ve been out of the RubyTalk loop for a couple of weeks.  
It’s like walking into the middle of a movie but not knowing you’ve  
missed  
the first half ;).

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [May 9, 2006, 11:24am UTC](https://www.ruby-forum.com/t/ruby-quiz-contest-winner/59937/6 "2006-05-09T11:24:45Z")

</div>

On Tue, 2006-05-09 at 04:01 +0900, James Edward G. II wrote:

> Ross B. has submitted a quiz that exceeded the mark I said I  
> would give a book to. Way to go Ross! (Thanks to everyone else who  
> entered as well.)

Wow - I never win _anything_ 🙂 Part of me wants to say I couldn’t take  
the prize, to let the contest run, but another part of me (the part  
that’s wanted this book for ages and still doesn’t have it ;)) just  
can’t do that… Thanks to everyone who coded up solutions, and big  
thanks to James for the contest, and for running the Ruby Q…

Here’s my own basic solution, modified slightly as we refined the  
problem:

#!/usr/local/bin/ruby  
def check\_parens(s, parens = ‘()[]{}’)  
stack = []  
s = s.gsub(/\[#{Regexp.escape(parens)}]/, ‘’)  
s.split(//).each\_with\_index do |c,i|  
if tp = parens.index©  
if tp == (pg\_start = (tp / 2) \* 2)  
# opening paren  
stack \<\< [c,i]  
else  
# closing paren  
if (stack.last || []).first == parens[pg\_start,1]  
stack.pop  
else  
stack \<\< [c,i]  
break  
end  
end  
end  
end  
stack  
end

def balanced\_and\_valid?(s, parens = ‘()[]{}’)  
!!if check\_parens(s,parens).empty?  
rx = /#{parens.scan(/…/).map { |e| Regexp.escape(e) }.join(’|’)}/  
true unless s =~ rx  
end  
end

if $0 == **FILE**  
raise ArgumentError, ‘No input’, [] unless brackets = ARGV.first  
exit(balanced\_and\_valid?(brackets) ? 0 : 1)  
end

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [May 9, 2006, 3:27pm UTC](https://www.ruby-forum.com/t/ruby-quiz-contest-winner/59937/7 "2006-05-09T15:27:21Z")

</div>

On May 9, 2006, at 4:23 AM, Ross B. wrote:

> Part of me wants to say I couldn’t take  
> the prize, to let the contest run…

I too have enjoyed the contest and it just so happens that I have  
another book here… Let’s go one more round!

For those joining us late here’s the deal: submit a quiz that grabs  
15 or more solutions and I will mail you a signed copy of Best of  
Ruby Q… It’s that simple.

James Edward G. II
