Блог пользователя johnathan79717

Автор johnathan79717, 5 лет назад, По-английски,

Many thanks to brunoja. He is the main contributor now. Please try it out and give us feedback. You're also welcome to fork the project or join us as a collaborator.


Codeforces Parser v1.4.1


Codeforces is a website for competitive programming. It holds contests, so-called Codeforces Rounds, about every week.

This is a python program that parses the sample tests from the contest problem pages. For each problem, it generates the sample input/output files and a shell script to run sample tests.

You can also find this article here, http://codeforces.com/blog/entry/10416


./parse.py contest_number (e.g. ./parse.py 464)

Where 464 is the contest number, not the round number! Check the URL of the contest on your browser, that is the number you are supposed to use.


What will happen, for example, if ./parse.py 464 is executed?
  1. Directories 464/A, 464/B, 464/C, 464/D and so on are created depending on the contest number of problems.
  2. For each problem, main.cc is copied and renamed to the problem letter to the corresponding directory. You can put the path of your usual template in parse.py:20.
  3. Problem page is downloaded from Codeforces website, and parsed. Sample input/output files are generated, e.g. input1, output1, input2, output2 and so on. You can create your own test cases after that, just keep the same naming format as others test cases.
  4. A script test.sh is generated. You can use it to compile and run the sample tests after you finish coding. Just run ./test.sh in the problem directory.
What will happen if ./test.sh is executed?
  1. Compilation: g++ -g -std=c++0x -Wall main.cc. You can change the compile options in parse.py:21.
  2. Run each sample tests on your program (a.out), and check the output by diff. If it's correct, print Accepted, or else print the sample test that went wrong.

Collaborators and Versions:

List of CodeForces Collaborators:

If you have any suggestions and/or bugs drop a message!

Versions Changes:
  • 1.4.1: Minor fixes, such as typos, bugs and special characters handling.
  • 1.4: Changed how the parser gets the problems. During the competitions the page is slightly different. Fixed some invalid character on input and output causing the script to crash. Forcing a new line on the input/output if there is none. Fixed some line number information in this README file.
  • 1.3: Some minor fixes and code organizing. Also fixed some typos. Removed the sample from default input and output files.
  • 1.2: Fixed some typos and constants. Fetching contest info, printing contest name and problem names. The contest may now have more or less than 5 problems, it will auto detect. The script will now generate the template with the problem letter. Fixed test cases fetching. The script was stopping for escaped html characters, such as '&lt'. Fixed script to work with python 3.
  • 1.1: Cleaner generation of the test script, now it auto detects the test cases, making you able to create your own cases. Echo color output, for accepted we get a green message, otherwise it is red. Added the time measurement for running the test cases. For the runtime error case, it now outputs the input case. Created some constants, such as compile options. These user modifiable constants should be easily spotted at the first lines of the python script.
  • 1.0: Initial Version.
Todo, Bugs & Troubleshootings:
  • In OS X it is necessary to install the gnu-time to measure time.
  • This parser currently works only on Unix OSes. If you want to add Windows/Other support let us know.
  • Проголосовать: нравится  
  • +63
  • Проголосовать: не нравится  

5 лет назад, # |
Rev. 2   Проголосовать: нравится +3 Проголосовать: не нравится

Hi! Thanks for this, really helpful :). In the test.sh script there is a line before running your code: rm sample_*. That is deleting the sample cases before running them, is it correct?

Also, you may want to change -std=c++11 to -std=c++0x, it works for older gcc versions (like mine, gcc 4.6).

5 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

from where we can download this?

and can it run under windows or it needs Linux?

and thanks for your work, it looks great :D

5 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Hi, I can not use it for proxy authentication? Can you add some support for that? As you are using urlopen for downloading the problem statement, Urlopen does not allow it to have a proxy with user authentication. Do you have some workaround for this?

  • »
    5 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Hi, could you elaborate in what situation do you need proxy authentication? Thanks. This may work, but I haven't tried it yet.

    • »
      5 лет назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      Hi, I usually use internet on the linux pcs installed in our institute labs, So I need to use proxy authentication for that purpose. For now I have kind of patched the things by using FancyURLopener class in the urrlib. I will try to find a good solution for it. I saw your link, it was quite similar to what I was doing, Thank you

5 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

I think it would be fantastic if Codeforces provides an API to obtain the test cases to not have to parse the entire HTML.

4 года назад, # |
  Проголосовать: нравится +16 Проголосовать: не нравится

What is the license of your code? I have my own tester and I would like to use your parser. :)

4 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Hi! I just merged the branch I was using with some fixes and I thought it might be good to announce it here :). The current version is the 1.4.1. Thanks!

8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Oh, this is great! Thank you)

6 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Nice! Trick for Windows users:
1.download git
2.change #!/usr/bin/python -> #!C:/YourPath/python.exe and a.out -> a.exe in script
3.keep playing 3d pinball space cadet:)
PS: time command doesn't work properly in this shell

2 месяца назад, # |
Rev. 3   Проголосовать: нравится +8 Проголосовать: не нравится

Thanks a lot johnathan79717 and brunoja.

I'm using it with vim to improve workflow.

$ sudo mv parse.py /usr/bin/parse

And following functions in .vimrc (vim configuration file)

"My functions for gvim && competitive"

nnoremap <C-z> :cd %:p:h <bar> call Parse()<CR>

"self check test cases"
autocmd filetype cpp nnoremap <C-a> :cd %:p:h <bar> call TestCase()<CR>

"To check the solution, while keeping cpp file in buffer, press Ctrl+x"
autocmd filetype cpp nnoremap <C-x> :cd %:p:h <bar> !xterm -e "./test.sh; read"<CR><CR>

"To parse the contest, press Ctrl+z"
function! Parse()
    let new_contest = input('Enter codeforces contest code: ')
    exec '!xterm -e "parse ' . l:new_contest. ';read"'

"For addition test cases that you may want to add, press Ctrl+a"

function! TestCase()
	 let index = 1
	 while !empty(glob("input".index))
		  let index += 1
	 echo "Input case: " . index . " "
	 let in = input('Input case: ')
	 echo " "
	 let out = input('Output case: ')
	 echo " "
	 let confirm = input ('Are you sure of writing this test case (y/n) : ')
	 if confirm == 'y'
		  silent exec '!echo "' . in . '" > input' . index
		  silent exec '!echo "' . out . '" > output' . index

And little changes in parser.py, if you like.

language_params = {
        'c++14' : {
            'TEMPLATE'    : '/home/convict/temp.cpp',
            'COMPILE_CMD' : 'g++ -g -std=gnu++14 -Wall -Wall -Wextra -pedantic -Wshadow -Wformat=2 -Wfloat-equal -Wconversion -Wlogical-op -Wshift-overflow=2 -Wduplicated-cond -Wcast-qual -Wcast-align  -D_FORTIFY_SOURCE=2 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover -fstack-protector  -O2 $DBG',
            'RUN_CMD'     : './a.out'