Adam Akhtar wrote:
> the reg exp. looks good
You should never consider a regex good looking. regexes should be
avoided whenever possible in favor of String methods. Better solutions
have been posted.
I would not subscribe to that rule. Often regular expressions are
faster than pure String based approaches - it depends on the issue at
hand.
Robert Klemme wrote:
> I would use another algorithm because of efficiency:
>
> list.delete_if
Repeatedly deleting elements from the middle of an array is certainly
not efficient. Also, suppose the results are:
[Adam Bobby wILd]
Looking at the results, you would have no way of knowing whether there
were duplicates spelled: adam, bobby, and wild. Therefore, the case of
the results appears to be irrelevant. If the case of the results is
irrelevant, then just providing the set is enough:
Alternatively one could use a Hash to preserve all spellings:
irb(main):001:0> list = %w{adam Adam bobby Bobby wild wILd}.sort_by { rand }
=> ["wild", "wILd", "Adam", "adam", "Bobby", "bobby"]
irb(main):002:0> res = Hash.new {|h,k| h[k]=}
=> {}
irb(main):003:0> list.each {|w| res[w.downcase] << w}
=> ["wild", "wILd", "Adam", "adam", "Bobby", "bobby"]
irb(main):004:0> res
=> {"bobby"=>["Bobby", "bobby"], "wild"=>["wild", "wILd"],
"adam"=>["Adam", "adam"]}
irb(main):005:0>
It all depends...
Cheers
robert
···
2008/2/12, 7stud -- <bbxx789_05ss@yahoo.com>:
--
use.inject do |as, often| as.you_can - without end