Ollie.marshall1-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2009-Dec-15 23:41 UTC
Help writing a simple algorithm
Hi,
I really need some help implementing an algorithm into my app!
I am trying to work out the popularity of books digg style which users
can vote on. To do this I was think this algorithm might work for the
time being :
Popularity = [ (V1/A1) + (V2/A2) + … + (Vn/An) ] / Book entry Age
Vn is a vote, and An is the age of that vote (for example, in minutes)
and then finally divided by the total time the entry has been on the
website.
What I therefore need help with is the implementation. Firstly will
the code belong in the model or should I create a library and call
that? I need the code to loop through the books votes (self.votes) and
substitute it in the algorithm.
Here''s where I got to:
def popularity
votes = Array.new
res = self.votes
res.each do |item|
vote = Hash.new
vote[:date] = item.created_at
votes << vote
end
return votes
end
I have no idea how I should move on from here? Any suggestions would
really be appreciated!
Thanks
Ollie
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Dec 15, 2009, at 3:41 PM, Ollie.marshall1-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org wrote:> Hi, > I really need some help implementing an algorithm into my app! > I am trying to work out the popularity of books digg style which users > can vote on. To do this I was think this algorithm might work for the > time being : > Popularity = [ (V1/A1) + (V2/A2) + … + (Vn/An) ] / Book entry Age > Vn is a vote, and An is the age of that vote (for example, in minutes) > and then finally divided by the total time the entry has been on the > website. > What I therefore need help with is the implementation. Firstly will > the code belong in the model or should I create a library and call > that?The code belongs in the Book model.> I need the code to loop through the books votes (self.votes) and > substitute it in the algorithm. > Here''s where I got to: > def popularity > votes = Array.new > res = self.votes > res.each do |item| > vote = Hash.new > > vote[:date] = item.created_at > > votes << vote > end > > return votes > end > I have no idea how I should move on from here? Any suggestions would > really be appreciated!I wouldn''t do any looping in Ruby at all. It''s going to get awful slow when the next Harry Potter book comes out and a million people vote for it... def popularity votes.sum(''vote_column / db_function_to_convert_time_to_minutes(minutes_column)'').to_f / (entered_at.to_i/60.0) end That keeps it all in the database... Haven''t tested it, but something along those lines should work I would think. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.