Skip to content

Commit

Permalink
#370: fix bugs around the indices of problems at Codeforces
Browse files Browse the repository at this point in the history
-   `0` exists but `1` doesn't
-   lower case is allowed
-   `F1` and `F2` (https://codeforces.com/contest/1133/problem/F1)
  • Loading branch information
kmyk committed Mar 22, 2019
1 parent 5d224d4 commit a20f876
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
21 changes: 15 additions & 6 deletions onlinejudge/service/codeforces.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ class CodeforcesProblem(onlinejudge.type.Problem):

def __init__(self, contest_id: int, index: str, kind: Optional[str] = None):
assert isinstance(contest_id, int)
assert index in string.ascii_uppercase
assert 1 <= len(index) <= 2
assert index[0] in string.ascii_uppercase
if len(index) == 2:
assert index[1] in string.digits
assert kind in (None, 'contest', 'gym', 'problemset')
self.contest_id = contest_id
self.index = index
Expand Down Expand Up @@ -189,15 +192,21 @@ def from_url(cls, url: str) -> Optional['CodeforcesProblem']:
result = urllib.parse.urlparse(url)
if result.scheme in ('', 'http', 'https') \
and result.netloc == 'codeforces.com':
# "0" is needed. example: https://codeforces.com/contest/1000/problem/0
# "[1-9]?" is sometime used. example: https://codeforces.com/contest/1133/problem/F2
re_for_index = r'(0|[A-Za-z][1-9]?)'
table = {}
table['contest'] = r'^/contest/([0-9]+)/problem/([0A-Za-z])$' # example: https://codeforces.com/contest/538/problem/H
table['problemset'] = r'^/problemset/problem/([0-9]+)/([0A-Za-z])$' # example: https://codeforces.com/problemset/problem/700/B
table['gym'] = r'^/gym/([0-9]+)/problem/([0A-Za-z])$' # example: https://codeforces.com/gym/101021/problem/A
normalize = (lambda c: c == '0' and 'A' or c.upper())
table['contest'] = r'^/contest/([0-9]+)/problem/{}$'.format(re_for_index) # example: https://codeforces.com/contest/538/problem/H
table['problemset'] = r'^/problemset/problem/([0-9]+)/{}$'.format(re_for_index) # example: https://codeforces.com/problemset/problem/700/B
table['gym'] = r'^/gym/([0-9]+)/problem/{}$'.format(re_for_index) # example: https://codeforces.com/gym/101021/problem/A
for kind, expr in table.items():
m = re.match(expr, utils.normpath(result.path))
if m:
return cls(int(m.group(1)), normalize(m.group(2)), kind=kind)
if m.group(2) == '0':
index = 'A'
else:
index = m.group(2).upper()
return cls(int(m.group(1)), index, kind=kind)
return None


Expand Down
15 changes: 15 additions & 0 deletions tests/service_codeforces.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ def test_from_url(self):
self.assertEqual(CodeforcesProblem.from_url('http://codeforces.com/gym/101021/problem/A').index, 'A')
self.assertEqual(CodeforcesProblem.from_url('https://codeforces.com/contest/1080/problem/A').contest_id, 1080)
self.assertEqual(CodeforcesProblem.from_url('https://codeforces.com/contest/1080/problem/A').index, 'A')
self.assertIsNone(CodeforcesProblem.from_url('https://atcoder.jp/contests/abc120/tasks/abc120_c'))

def test_from_url_corner_cases(self):
# 0
self.assertEqual(CodeforcesProblem.from_url('https://codeforces.com/contest/1105/problem/0').index, 'A')
self.assertEqual(CodeforcesProblem.from_url('https://codeforces.com/contest/1105/problem/1'), None)

# lower case
self.assertEqual(CodeforcesProblem.from_url('https://codeforces.com/contest/1110/problem/H').index, 'H')
self.assertEqual(CodeforcesProblem.from_url('https://codeforces.com/contest/1110/problem/h').index, 'H')

# F2
self.assertEqual(CodeforcesProblem.from_url('https://codeforces.com/contest/1133/problem/E').index, 'E')
self.assertEqual(CodeforcesProblem.from_url('https://codeforces.com/contest/1133/problem/F1').index, 'F1')
self.assertEqual(CodeforcesProblem.from_url('https://codeforces.com/contest/1133/problem/F2').index, 'F2')

0 comments on commit a20f876

Please sign in to comment.