Пример #1
0
    def _load_details(self,
                      session: Optional[requests.Session] = None) -> None:
        session = session or utils.new_default_session()

        # get
        resp = _request('GET',
                        self.get_url(type='beta', lang='ja'),
                        session=session)
        soup = bs4.BeautifulSoup(resp.content.decode(resp.encoding),
                                 utils.html_parser)

        # parse
        h2 = soup.find('span', class_='h2')
        self._alphabet, _, self._task_name = h2.text.partition(' - ')
        time_limit, memory_limit = h2.find_next_sibling('p').text.split(' / ')
        self._time_limit_msec = int(
            utils.remove_suffix(utils.remove_prefix(time_limit, '実行時間制限: '),
                                ' sec')) * 1000
        self._memory_limit_byte = int(
            utils.remove_suffix(utils.remove_prefix(memory_limit, 'メモリ制限: '),
                                ' MB')) * 1000 * 1000
        task_statement = soup.find('div', id='task-statement')
        p = task_statement.find('p')  # first
        if p is not None and p.text.startswith('配点 : '):
            self._score = int(
                utils.remove_suffix(utils.remove_prefix(p.text, '配点 : '),
                                    ' 点'))
        self._score_checked = True
Пример #2
0
def _AtCoderProblemContent_parse_partial(
        soup: bs4.BeautifulSoup,
        problem: 'AtCoderProblem') -> AtCoderProblemContentPartial:
    h2 = soup.find('span', class_='h2')

    alphabet, _, name = h2.text.partition(' - ')

    time_limit, memory_limit = h2.find_next_sibling('p').text.split(' / ')
    for time_limit_prefix in ('実行時間制限: ', 'Time Limit: '):
        if time_limit.startswith(time_limit_prefix):
            break
    else:
        assert False
    time_limit_msec = int(
        float(
            utils.remove_suffix(
                utils.remove_prefix(time_limit, time_limit_prefix), ' sec')) *
        1000)

    for memory_limit_prefix in ('メモリ制限: ', 'Memory Limit: '):
        if memory_limit.startswith(memory_limit_prefix):
            break
    else:
        assert False
    memory_limit_byte = int(
        float(
            utils.remove_suffix(
                utils.remove_prefix(memory_limit, memory_limit_prefix), ' MB'))
        * 1000 * 1000)

    return AtCoderProblemContentPartial(alphabet, memory_limit_byte, name,
                                        problem, time_limit_msec)
Пример #3
0
 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]
Пример #4
0
def _AtCoderProblemContent_parse_score(
        soup: bs4.BeautifulSoup) -> Optional[int]:
    task_statement = soup.find('div', id='task-statement')
    p = task_statement.find('p')  # first
    if p is not None and p.text.startswith('配点 : '):
        return int(
            utils.remove_suffix(utils.remove_prefix(p.text, '配点 : '), ' 点'))
    return None
Пример #5
0
    def _from_html(cls, html: bytes, *, problem: 'AtCoderProblem', session: Optional[requests.Session] = None, response: Optional[requests.Response] = None, timestamp: Optional[datetime.datetime] = None) -> 'AtCoderProblemData':
        soup = bs4.BeautifulSoup(html, utils.html_parser)
        h2 = soup.find('span', class_='h2')

        alphabet, _, name = h2.text.partition(' - ')

        time_limit, memory_limit = h2.find_next_sibling('p').text.split(' / ')
        for time_limit_prefix in ('実行時間制限: ', 'Time Limit: '):
            if time_limit.startswith(time_limit_prefix):
                break
        else:
            assert False
        if time_limit.endswith(' msec'):
            time_limit_msec = int(utils.remove_suffix(utils.remove_prefix(time_limit, time_limit_prefix), ' msec'))
        elif time_limit.endswith(' sec'):
            time_limit_msec = int(float(utils.remove_suffix(utils.remove_prefix(time_limit, time_limit_prefix), ' sec')) * 1000)
        else:
            assert False

        for memory_limit_prefix in ('メモリ制限: ', 'Memory Limit: '):
            if memory_limit.startswith(memory_limit_prefix):
                break
        else:
            assert False
        if memory_limit.endswith(' KB'):
            memory_limit_byte = int(float(utils.remove_suffix(utils.remove_prefix(memory_limit, memory_limit_prefix), ' KB')) * 1000)
        elif memory_limit.endswith(' MB'):
            memory_limit_byte = int(float(utils.remove_suffix(utils.remove_prefix(memory_limit, memory_limit_prefix), ' MB')) * 1000 * 1000)
        else:
            assert False

        return AtCoderProblemData(
            alphabet=alphabet,
            html=html,
            memory_limit_byte=memory_limit_byte,
            name=name,
            problem=problem,
            response=response,
            session=session,
            time_limit_msec=time_limit_msec,
            timestamp=timestamp,
        )
Пример #6
0
 def _parse_score(cls, soup: bs4.BeautifulSoup) -> Optional[int]:
     task_statement = soup.find('div', id='task-statement')
     p = task_statement.find('p')  # first
     if p is not None and p.text.startswith('配点 : '):
         score = utils.remove_suffix(utils.remove_prefix(p.text, '配点 : '), ' 点')
         try:
             return int(score)
         except ValueError:
             # some problems have scores like "<p>配点 : \(100\) 点</p>", not "<p>配点 : 100 点</p>"
             # example: https://atcoder.jp/contests/wupc2019/tasks/wupc2019_a
             pass
     return None