Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Measure CPSSD's contributions to Ireland's score on open.kattis.com
#!/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