[QUIZ] Internal Rate of Return (#156)

T

Todd Benson

I could only come up with an iterative solution also...

h = Hash['Hello', 1, 'Goodbye', 2, 'Hello there', 3]
a = h.select {|k, v| k=~/Hello/}.map {|v| v[1]}
p a

Another one just for fun...

h.map {|k,v| v if k=~/Hello/}.compact

Todd
 
R

Robert Klemme

2008/2/12 said:
oops i took the easy way and replied. sorry.
Thanks for the help/explanation. I was hoping there was a shorter/
more efficient way than iterating maybe a nested hash of some sort.

Nested Hashes would only help if you would know beforehand according
to which criteria you want to split up your strings. If you are doing
it on word basis you could do this

require 'set'
word_index = Hash.new {|h,k| h[k] = Set.new}
x.each_key {|k| k.downcase.scan(/\w+/) {|wd| word_index[wd] << k}}

Note though that you have to update word_index whenever x changes.

Well, there are indexing structures optimized for text retrieval (your
favorite search engine is using those). However, I have no idea
whether there are libs that implement those in Ruby. You could try to
check RAA at http://raa.ruby-lang.org/ for such libs.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top