#!/usr/bin/python
import cgi
import operator
import urllib
from xml.etree import ElementTree
def header():
print 'Content-type: text/html'
print
print """
Problem Set Catalog
Problem Catalog
This is a complete list of currently available contest problems on
turing. See the main page
for instructions on submitting.
To sort by a particular field, please click on the column heading.
Note: problems with short names in
red designate ones for which we may have chosen a short name
that differs from that described in the problem writeup. Please
use our short name for the input file and the source
code.
short name |
full name |
region |
year |
success% |
comments |
"""
def body():
tree = ElementTree.parse(urllib.urlopen('http://cs.slu.edu/~goldwasser/icpc/general/problems.xml'))
form = cgi.FieldStorage()
rowlist = []
for problem in tree.getroot():
# add appropriate tuple to rowlist
a = problem.find('abbrev')
if a is not None: # otherwise disregard entirely
short = abbrev = a.text.strip()
original = a.attrib.get('original')
if original is not None:
abbrev = "" + abbrev + ""
href = problem.findtext('url')
if not href:
archive = problem.find('archive')
if archive is not None:
cat = archive.findtext('category')
prob = archive.findtext('problem')
href = "http://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=%s&page=show_problem&problem=%s" % (cat,prob)
else:
uva = problem.findtext('uva')
if uva:
href = "http://acmicpc-live-archive.uva.es/nuevoportal/data/problem.php?p=%s"%uva
if href:
abbrev = '%s'%(href,abbrev)
full = problem.findtext('fullname')
if full is None:
full = ''
else:
full = full.strip()
region = problem.findtext('region')
if region is None:
region = ''
else:
region = region.strip()
ordinal = problem.findtext('ordinal')
try:
ordinal = int(ordinal)
except:
ordinal = None
year = problem.findtext('year')
if year is None:
year = ''
else:
year = year.strip()
success = problem.findtext('success')
if not success:
success = ''
else:
success = success.strip() + '%'
keywords = []
for k in problem.findall('keyword'):
if k.text is not None:
keywords.append(k.text.strip())
comments = ', '.join(keywords)
rowlist.append( (short,abbrev,full,region,year,success,comments,ordinal) )
# default order
rowlist.sort()
if form.has_key('sort'):
sortfield = form['sort'].value.strip()
if sortfield == 'year':
rowlist.sort(key=lambda x : x[3] if x[3].strip() else 'ZZZZZ')
rowlist.sort(key=operator.itemgetter(4), reverse=True)
elif sortfield == 'region':
rowlist.sort(key=operator.itemgetter(7)) # use ordinal
rowlist.sort(key=operator.itemgetter(4), reverse=True)
rowlist.sort(key=lambda x : x[3] if x[3].strip() else 'ZZZZZ')
elif sortfield == 'success':
rowlist.sort(key=lambda x : float(x[5][:-1] if '%' in x[5] else -1), reverse=True)
elif sortfield == 'name':
rowlist.sort(key=lambda x : x[2] if x[2].strip() else 'ZZZZZ')
for row in rowlist:
print """
%s |
%s |
%s |
%s |
%s |
%s |
""" % row[1:7]
def footer():
print """
"""
# main flow...
header()
body()
footer()