Skip to content

Instantly share code, notes, and snippets.

@alfg
Created April 24, 2012 23:13
Show Gist options
  • Save alfg/2484505 to your computer and use it in GitHub Desktop.
Save alfg/2484505 to your computer and use it in GitHub Desktop.
Reddit ranking algorithm
#Rewritten code from /r2/r2/lib/db/_sorts.pyx
from datetime import datetime, timedelta
from math import log
epoch = datetime(1970, 1, 1)
def epoch_seconds(date):
"""Returns the number of seconds from the epoch to date."""
td = date - epoch
return td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)
def score(ups, downs):
return ups - downs
def hot(ups, downs, date):
"""The hot formula. Should match the equivalent function in postgres."""
s = score(ups, downs)
order = log(max(abs(s), 1), 10)
sign = 1 if s > 0 else -1 if s < 0 else 0
seconds = epoch_seconds(date) - 1134028003
return round(order + sign * seconds / 45000, 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment