Thread: Google CodeJam?
View Single Post
  #21 (permalink)  
Old 04-24-2012, 05:08 AM
Hugh Aguilar
Guest
 
Posts: n/a
Default Re: Google CodeJam?

On Apr 23, 1:27*pm, "WJ" <w_a_x_...@yahoo.com> wrote:
> Hugh Aguilar wrote:
> > On Apr 21, 10:03*pm, "WJ" <w_a_x_...@yahoo.com> wrote:
> > > Ruby:

>
> > > _, numwords, numpatterns = gets().split.map{|s| s.to_i}

>
> > > words = (1 .. numwords).map{ gets().strip }
> > > patterns = (1 .. numpatterns).map{ gets().strip }

>
> > > patterns.each_with_index{|pat,i|
> > > * regex = Regexp.new( pat.gsub("(", "[").gsub(")", "]") )
> > > * printf "Case #%d: %d\n", i, words.grep(...

>
> > I don't know anything about Ruby, but I can certainly admire the
> > conciseness of your program. How does it compare in speed to mine?

>
> It takes 6.5 seconds for the large input file on my laptop.
>
> The program is run this way:
>
> ruby code-jam.rb Code-jam.in
>
> I'll explain a few things.
>
> _, numwords, numpatterns = gets().split.map{|s| s.to_i}
>
> gets() reads a line from stdin or the file given on the
> command-line; .split splits that string on whitespace,
> yielding an array or list of strings;
> .map works as in Lisp, in this case converting each string
> in the array or list into an integer.
>
> words = (1 .. numwords).map{ gets().strip }
>
> (1 .. numwords) is a range; some examples of ranges
> (the .to_a expands the range into an array or list):
> * (1 .. 5).to_a
> * * * ==>[1, 2, 3, 4, 5]
> * ("b" .. "f").to_a
> * * * ==>["b", "c", "d", "e", "f"]
> Here I'm simply using the range to read numwords lines
> from the file. *I could have done something like
> * words = []
> * numwords.times{ words << gets().strip }
> .strip removes all whitespace from beginning and end
> of the string.
>
> patterns.each_with_index{|pat,i|
>
> Iterating through the patterns; pat, of course, is
> the pattern. *i is assigned the index of the current
> item, starting with 0 (which means my output has an
> off-by-1 error).
>
> * printf "Case #%d: %d\n", i, words.grep( regex ).size
>
> .grep works on an array or list of strings, selecting
> only the items that match the regular expression.


Ruby is pretty impressive; I should learn more about it. BTW, do you
know Icon?

As I've said before, my Straight Forth will only be for micro-
controllers. I will have a "sister language" that will be used for
desktop-computer programs that are related in some way to the micro-
controller programs. I had been planning on Racket, but maybe I should
consider Ruby instead. Ruby is a lot more popular, although I think
that Racket has more novice-oriented documentation (a good thing, as
most micro-controller enthusiasts are more interested in hardware than
in software; they don't want to learn something complicated even if it
is more powerful, especially for desktop-computer programming which is
only peripherally related to micro-controllers).

Over on comp.lang.lisp, I've heard Ruby described as "Matz-Lisp" ---
meaning that Ruby is just Lisp with a more friendly syntax (for people
accustomed to infix). Would you consider that to be an accurate
description of Ruby?
Reply With Quote