import urllib, json, lxml, os
from collections import namedtuple
from lxml import etree
def file_put(fn, text):
with open(fn, 'wb') as fp:
fp.write(text)
def file_get(fn):
with open(fn, 'rb') as fp:
s = fp.read()
return s
def http_get(url, type='html'):
cache = urllib.quote_plus(url) + '.' + type
if not os.path.exists(cache):
s = urllib.urlopen(url).read()
file_put(cache, s)
return file_get(cache)
tc = http_get('http://www.topcoder.com/tc?module=BasicData&c=dd_coder_list', 'xml')
cf = http_get('http://codeforces.ru/api/user.ratedList?activeOnly=false', 'json')
user = namedtuple('user', 'tc cf')
users = {}
tc_items = etree.XML(tc).xpath('//row')
cf_items = json.loads(cf)['result']
for item in tc_items:
handle = item.xpath('handle/text()')[0].lower()
rating = int(item.xpath('alg_rating/text()')[0])
users[handle] = user(rating, 0)
for item in cf_items:
handle = item['handle'].lower()
rating = item['rating']
if handle in users:
users[handle] = users[handle]._replace(cf=rating)
else:
users[handle] = user(0, rating)
tcarr, cfarr = [], []
for handle in users:
if users[handle].tc and users[handle].cf:
tcarr.append(users[handle].tc)
cfarr.append(users[handle].cf)
print 'TC data bytes: %d, CF data bytes: %d' % (len(tc), len(cf))
print 'TC users: %d, CF users: %d' % (len(tc_items), len(cf_items))
print 'Common users (case insensitive): %d' % len(tcarr)
a, b = polyfit(tcarr, cfarr, 1)
eq = b / (1 - a)
print 'cf = %f * (tc - %f) + %f' % (a, eq, eq)
print 'cf = %f * (tc - %f) + %f' % (a, average(tcarr), average(cfarr))
matplotlib.rcParams.update({'font.family': 'Tahoma'})
figure(figsize=(16,10))
plot(tcarr, cfarr, '.')
xx = linspace(0, max(tcarr))
plot(xx, a * xx + b, 'r')
title(u'Корреляция рейтинга на TopCoder и CodeForces')
xlabel('TopCoder')
ylabel('CodeForces')
grid()
axes().set_aspect('equal')
show()
TC data bytes: 22865891, CF data bytes: 13256210 TC users: 65819, CF users: 53414 Common users (case insensitive): 10501 cf = 0.469156 * (tc - 1968.218227) + 1968.218227 cf = 0.469156 * (tc - 1142.075707) + 1580.628702
from IPython.display import HTML
HTML('''
<script type="text/javascript">
function calc(n) {
cf = %f * n + %f;
tc = %f * n + %f;
alert('TC rating ' + n + ' => CF rating ~' + Math.round(cf) + '\\n' + 'CF rating ' + n + ' => TC rating ~' + Math.round(tc));
}
</script>
<input type="text" id="rating" value="1500">
<button onclick="calc(document.getElementById('rating').value)">OK</button>
''' % (a, b, 1 / a, -b / a))