Algorithms

Moderators: None (Apply to moderate this forum)
Number of threads: 402
Number of posts: 786

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Ruby: Serve by weight Posted by imrancs on 1 Mar 2011 at 7:25 AM
For example I have this data:

headings = { 
         :heading1 => { :weight => 60, :show_count => 0}
         :heading2 => { :weight => 10, :show_count => 0}
         :heading3 => { :weight => 20, :show_count => 0}
         :heading4 => { :weight => 10, :show_count => 0}
       }
total_views = 0


Now I want to serve each heading based on their weightages. For instance, for first 10 requests/iterations, heading1, heading3, heading2 and heading4 would be served 6, 2, 1, and 1 times respectively in order (by weight).

For every iteration show_count of served heading will increment by one and total_views will also increment globally.

Could you please suggest an algorithm or some ruby code to handle this?




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.