Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NONE: support downloading samples from Facebook Hacker Cup #456

Merged
merged 1 commit into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions onlinejudge/_implementation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def get_parser() -> argparse.ArgumentParser:
PKU JudgeOnline
Kattis
Toph (Problem Archive)
Facebook Hacker Cup

supported services with --system:
Aizu Online Judge
Expand Down
1 change: 1 addition & 0 deletions onlinejudge/service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import onlinejudge.service.atcoder
import onlinejudge.service.codeforces
import onlinejudge.service.csacademy
import onlinejudge.service.facebook
import onlinejudge.service.hackerrank
import onlinejudge.service.kattis
import onlinejudge.service.poj
Expand Down
80 changes: 80 additions & 0 deletions onlinejudge/service/facebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Python Version: 3.x
# -*- coding: utf-8 -*-
"""
the module for Facebook Hacker Cup (https://www.facebook.com/hackercup/)
"""

import urllib.parse
from typing import *

import requests

import onlinejudge._implementation.logging as log
import onlinejudge._implementation.utils as utils
import onlinejudge.dispatch
import onlinejudge.type
from onlinejudge.type import TestCase


class FacebookHackerCupService(onlinejudge.type.Service):
def get_url(self) -> str:
return 'https://www.facebook.com/hackercup/'

def get_name(self) -> str:
return 'Facebook Hacker Cup'

@classmethod
def from_url(cls, url: str) -> Optional['FacebookHackerCupService']:
# example: https://www.facebook.com/hackercup/
result = urllib.parse.urlparse(url)
if result.scheme in ('', 'http', 'https') \
and result.netloc == 'www.facebook.com' \
and utils.normpath(result.path).startswith('/hackercup'):
return cls()
return None


class FacebookHackerCupProblem(onlinejudge.type.Problem):
"""
:ivar problem_id: :py:class:`int`
"""

def __init__(self, *, problem_id: int):
self.problem_id = problem_id

def download_sample_cases(self, session: Optional[requests.Session] = None) -> List[TestCase]:
session = session or utils.get_default_session()
url_format = 'https://www.facebook.com/hackercup/example/?problem_id={}&type={}'
resp_in = utils.request('GET', url_format.format(self.problem_id, 'input'), session=session)
resp_out = utils.request('GET', url_format.format(self.problem_id, 'output'), session=session)
sample = TestCase(
'sample',
utils.remove_prefix(resp_in.headers['Content-Disposition'], 'attachment;filename='),
resp_in.content,
utils.remove_prefix(resp_out.headers['Content-Disposition'], 'attachment;filename='),
resp_out.content,
)
return [sample]

def get_url(self) -> str:
return 'https://www.facebook.com/hackercup/problem/{}/'.format(self.problem_id)

@classmethod
def from_url(cls, url: str) -> Optional['FacebookHackerCupProblem']:
# example: https://www.facebook.com/hackercup/problem/448364075989193/
result = urllib.parse.urlparse(url)
if result.scheme in ('', 'http', 'https') \
and result.netloc == 'www.facebook.com' \
and utils.normpath(result.path).startswith('/hackercup/problem/'):
dirs = utils.normpath(result.path).split('/')
if 3 < len(dirs) and dirs[3].isdigit():
problem_id = int(dirs[3])
return cls(problem_id=problem_id)
return None

def get_service(self) -> FacebookHackerCupService:
return FacebookHackerCupService()


onlinejudge.dispatch.services += [FacebookHackerCupService]
onlinejudge.dispatch.problems += [FacebookHackerCupProblem]
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Tools for online judge services. Downloading sample cases, Testing/Submitting yo
- PKU JudgeOnline
- Kattis
- Toph (Problem Archive)
- Facebook Hacker Cup
- Download system test cases
- yukicoder
- Aizu Online Judge
Expand Down
58 changes: 58 additions & 0 deletions tests/service_facebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import inspect
import unittest

from onlinejudge.service.facebook import FacebookHackerCupProblem, FacebookHackerCupService
from onlinejudge.type import TestCase


class FacebookHackerCupSerivceTest(unittest.TestCase):
def test_from_url(self):
self.assertIsInstance(FacebookHackerCupService.from_url('https://www.facebook.com/hackercup/'), FacebookHackerCupService)
self.assertIsInstance(FacebookHackerCupService.from_url('https://www.facebook.com/hackercup/problem/448364075989193/'), FacebookHackerCupService)
self.assertIsNone(FacebookHackerCupService.from_url('https://www.facebook.com/AtCoder/'))


class FacebookHackerCupProblemTest(unittest.TestCase):
def test_from_url(self):
self.assertEqual(FacebookHackerCupProblem.from_url('https://www.facebook.com/hackercup/problem/448364075989193/').problem_id, 448364075989193)

def test_download_samples(self):
problem = FacebookHackerCupProblem.from_url('https://www.facebook.com/hackercup/problem/2390352741015547/')
sample_input = (inspect.cleandoc("""
6
2 2 2
6 3 0 0 0 10
2 4 0 0 0 10
2 2 1
6 3 0 0 0 10
2 4 0 0 0 10
2 0 1
6 3 0 0 0 10
2 4 0 0 0 10
4 1 3
1 1 1 0 0 2
1 2 1 0 1 2
10 7 5
15 19 34 41 32 44
34 3 25 2 17 38
10000 6011 4543
434894347 263348046 2565 3970 1267 622277910
524251054 294718567 0 1 3718 689139248
""") + '\n').encode()
sample_output = (inspect.cleandoc("""
Case #1: 4
Case #2: 5
Case #3: -1
Case #4: 3
Case #5: 45
Case #6: 766791757
""") + '\n').encode()
self.assertEqual(problem.download_sample_cases(), [
TestCase(
'sample',
'connect_the_dots_sample_input.txt',
sample_input,
'connect_the_dots_sample_output.txt',
sample_output,
),
])