Skip to content

Instantly share code, notes, and snippets.

@soopercorp
Created February 20, 2012 23:38
Show Gist options
  • Save soopercorp/1872317 to your computer and use it in GitHub Desktop.
Save soopercorp/1872317 to your computer and use it in GitHub Desktop.
bestthing.info algorithm

This is a simple algorithm as described by bestthings.info. An entity's rank is calculated by (Num_things_I_beat)/(Num_things_I_beat + Num_things_beat_me)

I put this together in about half an hour (I'm really starving for time right now, with tons of projects!). I have a few ideas for the second part, but alas no code.

Part 2) We will aim to improve the accuracy of our ranking by trying to re-rank those pairs that are not different enough. For example, if the agg between a pair is say < 10, they are too similar, and we would wish to re-rank them. This could be done by implementing a simple dictionary to store aggregate ranks..

aggregate = {('a','b'): 23, ('a','c'): 3}

Now we could scan this dict to present to a user those pairs that are not different enough. For those with very high aggregate scores, we can avoid re-ranking because "the voters have had their say."

#!/usr/bin/env python
import cPickle as pickle
import random
# things I beat
tIB = {}
# things beat me
tBM = {}
numObj = random.randint(10,100)
corpus = {}
# Constructing the corpus
for i in range(numObj):
name = str(i)
corpus[name] = {}
for j in range(numObj):
if(j!=i):
opp = str(j)
corpus[name][opp] = random.randint(20,5000)
# picklable for portability
pickle.dump( corpus, open( "best.p", "wb" ) )
corpus = pickle.load(open("best.p","rb"))
# find who beat whom.
for k in corpus:
numObj += 1
tIB[k] = 0
tBM[k] = 0
for opp in corpus[k]:
myScore = corpus[k][opp]
oppScore = corpus[opp][k]
agg = myScore - oppScore
if(agg > 0):
tIB[k]+=1
elif(agg < 0):
tBM[k]+=1
else:
tIB[k]+=0.5
tBM[k]+=0.5
rank = {}
# Assign ranks
for x in tIB:
rank[x] = (float(tIB[x])/(float(tIB[x]+tBM[x])))
# Sort and print
for key,value in sorted(rank.iteritems(), key=lambda (k,v): (v,k)):
print "%s: %s" % (key, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment