Imagine,
I have
leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
for a given string, say "some stuff NL is chunky" i want determine which
of the matches it contains...
now, the hard way (more code, less thought) would be to iterate the
array and do a ~= on it...but is there a simpler way ???
thanks in advance
···
--
Posted via http://www.ruby-forum.com/ .
Robert_K1
(Robert K.)
27 February 2006 13:42
2
Imagine,
I have
leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
for a given string, say "some stuff NL is chunky" i want determine which
of the matches it contains...
now, the hard way (more code, less thought) would be to iterate the
array and do a ~= on it...but is there a simpler way ???
leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
=> ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
"some stuff NL is chunky".scan( Regexp.new( leagues.join('|') ) )
=> ["NL"]
Kind regards
robert
···
2006/2/27, mikkel <mikkel@helenius.dk>:
--
Have a look: Robert K. | Flickr
Imagine,
I have
leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
for a given string, say "some stuff NL is chunky" i want determine which
of the matches it contains...
now, the hard way (more code, less thought) would be to iterate the
array and do a ~= on it...but is there a simpler way ???
The hard way isn't too hard and doesn't require but a line of code:
>> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
=> ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
>> str="some stuff NL is chunky"
=> "some stuff NL is chunky"
>> leagues.find_all { |league| str.include? league }
=> ["NL"]
Hope that helps.
James Edward Gray II
···
On Feb 27, 2006, at 7:35 AM, mikkel wrote:
How about:
leagues = %w{1D 2D U16 U19 LR RR JNL NL}
words = "some stuff NL is chunky".split
leagues.select { |m| words.include?(m) } # => ["NL"]
-- Daniel
···
On Feb 27, 2006, at 2:35 PM, mikkel wrote:
now, the hard way (more code, less thought) would be to iterate the
array and do a ~= on it...but is there a simpler way ???
You've got a bunch of great answers already, but here's another option.
leagues = %w(1D 2D U16 U19 LR RR JNL NL)
words = "some stuff NL is chunky"
irb(main):008:0> words.split & leagues
=> ["NL"]
- Hitesh
http://www.jasani.org/
"Robert Klemme" <shortcutter@googlemail.com> writes:
Imagine,
I have
leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
for a given string, say "some stuff NL is chunky" i want determine which
of the matches it contains...
now, the hard way (more code, less thought) would be to iterate the
array and do a ~= on it...but is there a simpler way ???
leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
=> ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
"some stuff NL is chunky".scan( Regexp.new( leagues.join('|') ) )
=> ["NL"]
irb(main):002:0> "some stuff NL is chunky".scan Regexp.union(*leagues)
=> ["NL"]
···
2006/2/27, mikkel <mikkel@helenius.dk>:
robert
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
Robert
(Robert)
27 February 2006 17:53
8
Christian Neukirchen wrote:
"Robert Klemme" <shortcutter@googlemail.com> writes:
Imagine,
I have
leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
for a given string, say "some stuff NL is chunky" i want determine
which of the matches it contains...
now, the hard way (more code, less thought) would be to iterate the
array and do a ~= on it...but is there a simpler way ???
leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
=> ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
"some stuff NL is chunky".scan( Regexp.new( leagues.join('|') ) )
=> ["NL"]
irb(main):002:0> "some stuff NL is chunky".scan
Regexp.union(*leagues) => ["NL"]
Even better! Didn't know about that method. Learn something new every
day. Thanks!
robert
···
2006/2/27, mikkel <mikkel@helenius.dk>:
Actually if you flip it around as 'leagues & words.split' it turns out
to have some significant performance advantages in many cases. See
http://www.jasani.org/articles/2006/02/28/adding-the-science-back-to-computer-science
for more details.
- Hitesh
http://www.jasani.org/
amazing...
thanks a bunch everybody...
···
On Tuesday, February 28, 2006, at 2:53 AM, Robert Klemme wrote:
Christian Neukirchen wrote:
"Robert Klemme" <shortcutter@googlemail.com> writes:
2006/2/27, mikkel <mikkel@helenius.dk>:
Imagine,
I have
leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
for a given string, say "some stuff NL is chunky" i want determine
which of the matches it contains...
now, the hard way (more code, less thought) would be to iterate the
array and do a ~= on it...but is there a simpler way ???
leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
=> ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
"some stuff NL is chunky".scan( Regexp.new( leagues.join('|') ) )
=> ["NL"]
irb(main):002:0> "some stuff NL is chunky".scan
Regexp.union(*leagues) => ["NL"]
Even better! Didn't know about that method. Learn something new every
day. Thanks!
robert
Mikkel Bruun
www.strongside.dk - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk - Buy Old Stuff!(DK)
--
Posted with http://DevLists.com . Sign up and save your time!
Is it possible that link is incorrect?
"Firefox can't establish a connection to the server at www.jasani.org ."
I'm dying to learn about this now.
···
hitesh.jasani@gmail.com wrote:
Actually if you flip it around as 'leagues & words.split' it turns out
to have some significant performance advantages in many cases. See
http://www.jasani.org/articles/2006/02/28/adding-the-science-back-to-computer-science
for more details.
- Hitesh
http://www.jasani.org/
"hitesh.jasani@gmail.com" <hitesh.jasani@gmail.com> writes:
Actually if you flip it around as 'leagues & words.split' it turns out
to have some significant performance advantages in many cases. See
http://www.jasani.org/articles/2006/02/28/adding-the-science-back-to-computer-science
for more details.
You'd better cache those Regexps.
Also, test with longer "words"---they are more likely to grow than the
number of leagues.
···
- Hitesh
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
I am surprised by the scan failures ("could not continue test"). Do you know what causes the error?
···
hitesh.jasani@gmail.com wrote:
Actually if you flip it around as 'leagues & words.split' it turns out
to have some significant performance advantages in many cases. See
http://www.jasani.org/articles/2006/02/28/adding-the-science-back-to-computer-science
for more details.
Jeffrey, the link should be working for you now. My hosting provider
had a number of servers go belly up earlier in the day.
Good comments Christian. I thought I'd just hack a set of tests and
post some quick results, but all I think I did was prove that I'm not
supposed to be coding first thing in the morning.
Yes, once you fix that bug in the code by caching the Regexes, they
perform very impressively. In fact, for the modified tests I just ran,
they beat out every other solution in every case except for extremely
large league sizes (> 18,000 elements) where they wouldn't run at all.
But I'm loathe to draw any conclusions from the data just yet. (Burned
once, twice shy?)
There appears to be at least one other bug in the code. One astute,
anonymous person pointed out that the six solutions will not return the
same results for the generated datasets and that preprocessing of input
data could help improve performance even more. I think it all depends
on how one defines the problem as to whether the generated data is
valid input or undefined requirements now being levied on the code.
Mmmm.... I hope JEG II is watching this thread as I'm wondering if
there isn't a rubyquiz in here somewhere.
- Hitesh
http://www.jasani.org/
Jeffrey Schwab <jeff@schwabcenter.com> writes:
···
hitesh.jasani@gmail.com wrote:
Actually if you flip it around as 'leagues & words.split' it turns out
to have some significant performance advantages in many cases. See
http://www.jasani.org/articles/2006/02/28/adding-the-science-back-to-computer-science
for more details.
I am surprised by the scan failures ("could not continue test"). Do
you know what causes the error?
irb(main):002:0> Regexp.new "x"*600_000
RegexpError: regular expression too big: /xxxxx...
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
If you can think of a good way to spin it:
suggestion@rubyquiz.com
James Edward Gray II
···
On Feb 28, 2006, at 11:03 PM, hitesh.jasani@gmail.com wrote:
Mmmm.... I hope JEG II is watching this thread as I'm wondering if
there isn't a rubyquiz in here somewhere.