Last active
April 7, 2017 03:12
-
-
Save iandioch/ac633cf2e130b050ee1f6a7a565b6da2 to your computer and use it in GitHub Desktop.
Measure CPSSD's contributions to Ireland's score on open.kattis.com
This file contains hidden or 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
#!/usr/bin/python3 | |
# adapted from code at https://github.com/iandioch/ceres/ | |
import json | |
from urllib.request import urlopen, Request | |
from bs4 import BeautifulSoup | |
cpssds = set([ | |
'Noah Ó Donnaile', | |
'Cian Ruane', | |
'Stefan Kennedy', | |
'Brandon Ibbotson', | |
'Ross O\'Sullivan', | |
'Cliodhna Harrison', | |
'Ciaran Murphy', | |
'Lucas Savva', | |
'Oskar McDermott', | |
]) | |
def is_cpssd(name): | |
return name in cpssds | |
def load_page(url): | |
q = Request(url) | |
q.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Firefox') | |
q.add_header('Accept', 'text/html,application/xhtml+xml,applicationxml;q=0.9,*/*;q=0.8') | |
q.add_header('Accept-Encoding', 'none') | |
q.add_header('Accept-Language', 'en-IE,en,en-US') | |
q.add_header('Connection', 'keep-alive') | |
return urlopen(q).read() | |
def get_user_data(page): | |
soup = BeautifulSoup(page, 'html.parser') | |
data = {} | |
table = soup.find('table') | |
rows = table.find_all('tr') | |
data['global_rank'] = number_to_position(int(rows[1].find_all('td')[0].string)) | |
data['score'] = (rows[1].find_all('td')[1].string) | |
return data | |
def get_rank_data(page): | |
soup = BeautifulSoup(page, 'html.parser') | |
tables = soup.find_all('table') | |
score_table = tables[0] | |
ireland_tot = float(score_table.find_all('tr')[1].find_all('td')[1].string) | |
individual_table = tables[2] | |
rows = individual_table.find_all('tr') | |
data = [] | |
for row in rows[1:]: | |
parts = row.find_all('td') | |
pos = int(parts[0].string) | |
name = parts[1].find('a').string | |
score = float(parts[3].string) | |
data.append((name, pos, score)) | |
return ireland_tot, data | |
def mult(i): | |
return (0.8)**(i) | |
if __name__ == '__main__': | |
ireland_total, irish_data = get_rank_data(load_page('https://open.kattis.com/countries/IRL')) | |
cpssd_data = [(n, p, s) for n, p, s in irish_data if is_cpssd(n)] | |
cpssd_tot = 0 | |
print('Rank\tScore\t% Added\t# Added\tCumulative Tot\tName') | |
for n, p, s in cpssd_data: | |
m = 0.2*mult(p-1) | |
d = m*s | |
cpssd_tot += d | |
print('#{}\t{}\t{:4.1f}%\t{:.2f}\t({:6.2f})\t{}'.format(p, s, 100*m, d, cpssd_tot, n)) | |
print('CPSSD Total:\t{:.2f}'.format(cpssd_tot)) | |
print('Ireland Total:\t{:.2f}'.format(ireland_total)) | |
print('CPSSD Percent:\t{:.4f}'.format(100*cpssd_tot/ireland_total)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment