Forum: Ruby Ruby Quiz Contest Winner!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James G. (Guest)
on 2006-05-08 23:02
(Received via mailing list)
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
Belorion (Guest)
on 2006-05-08 23:55
(Received via mailing list)
On 5/8/06, James Edward G. II <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.
Logan C. (Guest)
on 2006-05-09 00:05
(Received via mailing list)
On May 8, 2006, at 3:54 PM, Belorion wrote:

> On 5/8/06, James Edward G. II <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
Belorion (Guest)
on 2006-05-09 00:11
(Received via mailing list)
On 5/8/06, Logan C. <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 ;).
James G. (Guest)
on 2006-05-09 00:11
(Received via mailing list)
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
Ross B. (Guest)
on 2006-05-09 13:24
(Received via mailing list)
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(c)
      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
James G. (Guest)
on 2006-05-09 17:27
(Received via mailing list)
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
This topic is locked and can not be replied to.