Last active
April 7, 2017 03:13
-
-
Save iandioch/1e7fbbc6c4346304aac297428c0a7b19 to your computer and use it in GitHub Desktop.
Measure DCU's contributions to Ireland's score on open.kattis.com
This file contains 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 | |
def is_dcu(c): | |
return c == 'Dublin City University' | |
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') | |
print(parts) | |
pos = int(parts[0].string) | |
name = parts[1].find('a').string | |
college = None | |
college_link = parts[2].find('a') | |
if college_link is not None: | |
college = college_link['title'] | |
score = float(parts[3].string) | |
data.append((name, pos, college, 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')) | |
dcu_data = [(n, p, c, s) for n, p, c, s in irish_data if is_dcu(c)] | |
dcu_tot = 0 | |
print('Rank\tScore\t% Added\t# Added\tCumulative Tot\tName') | |
for n, p, c, s in dcu_data: | |
m = 0.2*mult(p-1) | |
d = m*s | |
dcu_tot += d | |
print('#{}\t{}\t{:4.1f}%\t{:.2f}\t({:6.2f})\t{}'.format(p, s, 100*m, d, dcu_tot, n)) | |
print('DCU Total:\t{:.2f}'.format(dcu_tot)) | |
print('Ireland Total:\t{:.2f}'.format(ireland_total)) | |
print('DCU Percent:\t{:.4f}'.format(100*dcu_tot/ireland_total)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment