Created
April 22, 2012 17:24
-
-
Save sriprasanna/2465475 to your computer and use it in GitHub Desktop.
Reddit ranking algorithm in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/Users/dustyeike/.rvm/rubies/ruby-1.9.2-p290/bin/ruby | |
require "rubygems" | |
require "test/unit" | |
require "date" | |
EPOCH = Date.new(1970, 1, 1) | |
# | |
# Returns the number of seconds from the epoch to date | |
def epoch_seconds date | |
td = date - EPOCH | |
Time.at(td).day * 86400 + Time.at(td).sec + (Time.at(td).usec).to_f / 1000000 | |
end | |
def score ups, down | |
ups - down | |
end | |
# | |
# The hot formula. Should match the equivalent function in postgres. | |
def hot ups, down, date | |
s = score ups, down | |
order = Math.log([s.abs, 1].max, 10) | |
if s > 0 | |
sign = 1 | |
elsif s < 0 | |
sign = -1 | |
else | |
sign = 0 | |
end | |
seconds = epoch_seconds(date) - 1134028003 | |
(order + sign * seconds / 45000).round | |
end | |
class TestRedditAlgoritym < Test::Unit::TestCase | |
def setup | |
today = DateTime.now | |
@now = Date.new(today.year, today.month, today.day) | |
end | |
def test_epoch_seconds | |
assert_equal(epoch_seconds(@now), 2678403.0) | |
end | |
def test_score | |
assert_equal(score(10, 5), 5) | |
end | |
def test_hot | |
assert_equal(hot(10,5,@now), -25140) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As this is the 2nd result in Google and the gist has a typo and won't work properly, I should post the corrected gist in ruby(not made by me):
https://gist.github.com/jrochkind/2636355