Skip to content

Instantly share code, notes, and snippets.

@nanosplit
Forked from jrochkind/gist:2636355
Created December 12, 2016 03:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nanosplit/db42e507c5d1d984b664868db10a3669 to your computer and use it in GitHub Desktop.
Save nanosplit/db42e507c5d1d984b664868db10a3669 to your computer and use it in GitHub Desktop.
reddit 'hot' algorithm, in ruby, with typo fixed
require 'date'
# Actually doesn't matter WHAT you choose as the epoch, it
# won't change the algorithm. Just don't change it after you
# have cached computed scores. Choose something before your first
# post to avoid annoying negative numbers. Choose something close
# to your first post to keep the numbers smaller. This is, I think,
# reddit's own epoch.
$our_epoch = Time.local(2005, 12, 8, 7, 46, 43).to_time
def epoch_seconds(t)
(t.to_i - $our_epoch.to_i).to_f
end
# date is a ruby Time
def hot(ups, downs, date)
s = ups - downs
displacement = Math.log( [s.abs, 1].max, 10 )
sign = if s > 0
1
elsif s < 0
-1
else
0
end
return (displacement * sign.to_f) + ( epoch_seconds(date) / 45000 )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment