Exemplo n.º 1
0
def download_problem(contest_uri, problem):
    problem_uri = contest_uri + '/problem/' + problem
    print('Retrieving', problem_uri, '...')
    sys.stdout.flush()
    problem_html = urllib.request.urlopen(problem_uri).read().decode('utf-8')
    print('Retrieved problem {} ({} bytes).'.format(problem, len(problem_html)))

    # Hack for codeforces HTML errors
    problem_html = problem_html.replace('<p</p>', '<p></p>')
    problem_html = problem_html.replace('<ul</ul>', '<ul></ul>')
    problem_html = problem_html.replace('<div class="sample-test"<', '<div class="sample-test"><')

    parser = ProblemHTMLParser()
    try:
        parser.feed(problem_html)
    except:
        print(problem_html, file=sys.stderr)
        raise

    examples = parser.getExamples()

    problem_dir = problem.lower()
    if not os.path.isdir(problem_dir):
        os.mkdir(problem_dir)

    for i, example in enumerate(examples, 1):
        input_path = os.path.join(problem_dir, '{}.in.{}'.format(problem.lower(), i))
        with open(input_path, 'w') as f:
            f.write(example[0])

        output_path = os.path.join(problem_dir, '{}.out.{}'.format(problem.lower(), i))
        with open(output_path, 'w') as f:
            f.write(example[1])

    print('Wrote {} examples for problem {}.'.format(len(examples), problem))
Exemplo n.º 2
0
def download_problem(contest_uri, problem):
    problem_uri = contest_uri + '/problem/' + problem
    print('Retrieving', problem_uri, '...')
    sys.stdout.flush()
    problem_html = urllib.request.urlopen(problem_uri).read().decode('utf-8')
    print('Retrieved problem {} ({} bytes).'.format(problem,
                                                    len(problem_html)))

    # Hack for codeforces HTML errors
    problem_html = problem_html.replace('<p</p>', '<p></p>')
    problem_html = problem_html.replace('<ul</ul>', '<ul></ul>')
    problem_html = problem_html.replace('<div class="sample-test"<',
                                        '<div class="sample-test"><')

    parser = ProblemHTMLParser()
    try:
        parser.feed(problem_html)
    except:
        print(problem_html, file=sys.stderr)
        raise

    examples = parser.getExamples()

    for i, example in enumerate(examples, 1):
        input_path = INPUT_TESTCASE_FORMAT.format(problem=problem, number=i)
        write_example(input_path, example[0])

        output_path = OUTPUT_TESTCASE_FORMAT.format(problem=problem, number=i)
        write_example(output_path, example[1])

    print('Wrote {} examples for problem {}.'.format(len(examples), problem))
Exemplo n.º 3
0
def download_problem(contest_uri, problem):
    problem_uri = contest_uri + '/problem/' + problem
    print('Retrieving', problem_uri, '...')
    sys.stdout.flush()
    problem_html = urllib.request.urlopen(problem_uri).read().decode('utf-8')
    print('Retrieved problem {} ({} bytes).'.format(problem, len(problem_html)))

    # Hack for codeforces HTML errors
    problem_html = problem_html.replace('<p</p>', '<p></p>')
    problem_html = problem_html.replace('<ul</ul>', '<ul></ul>')
    problem_html = problem_html.replace('<div class="sample-test"<', '<div class="sample-test"><')

    parser = ProblemHTMLParser()
    try:
        parser.feed(problem_html)
    except:
        print(problem_html, file=sys.stderr)
        raise

    examples = parser.getExamples()

    problem_dir = problem.lower()
    if not os.path.isdir(problem_dir):
        os.mkdir(problem_dir)

    for i, example in enumerate(examples, 1):
        input_path = os.path.join(problem_dir, 'in{}'.format(i))
        with open(input_path, 'w') as f:
            f.write(example[0])

        output_path = os.path.join(problem_dir, 'out{}'.format(i))
        with open(output_path, 'w') as f:
            f.write(example[1])

    print('Wrote {} examples for problem {}.'.format(len(examples), problem))
Exemplo n.º 4
0
    problem_html = urllib.request.urlopen(problem_uri).read().decode('utf-8')
    print('OK ({} bytes).'.format(len(problem_html)))

    # Hack for codeforces HTML errors
    problem_html = problem_html.replace('<p</p>', '<p></p>')
    problem_html = problem_html.replace('<ul</ul>', '<ul></ul>')
    problem_html = problem_html.replace('<div class="sample-test"<', '<div class="sample-test"><')
    
    parser = ProblemHTMLParser()
    try:
        parser.feed(problem_html)
    except:
        print(problem_html, file=sys.stderr)
        raise

    examples = parser.getExamples()

    problem_dir = problem.lower()
    if not os.path.isdir(problem_dir):
        os.mkdir(problem_dir)

    for i, example in enumerate(examples, 1):
        input_path = os.path.join(problem_dir, 'in{}'.format(i))
        with open(input_path, 'w') as f:
            f.write(example[0])

        output_path = os.path.join(problem_dir, 'out{}'.format(i))
        with open(output_path, 'w') as f:
            f.write(example[1])

    print('Wrote {} examples for problem {}.'.format(len(examples), problem))