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.
https://github.com/johnathan79717/codeforces-parser
Codeforces Parser v1.4.1
Summary
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.me/blog/entry/10416
Example:
./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.
Effect:
What will happen, for example, if ./parse.py 464
is executed?
- Directories
464/A
,464/B
,464/C
,464/D
and so on are created depending on the contest number of problems. - 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 inparse.py:20
. - 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. - 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?
- Compilation:
g++ -g -std=c++0x -Wall main.cc
. You can change the compile options inparse.py:21
. - Run each sample tests on your program (
a.out
), and check the output bydiff
. 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 '<'. 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.
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).
Thanks, that's a stupid mistake. I've removed it. Also changed to -std=c++0x. Thanks a lot for the suggestion :)
it is better to replace on "rm -R my_output{0}\n"
Your right! That's what I had in mind when I wrote that line I guess :P Thanks a lot.
You should not remove the a.out! Also add a -R to the rm command, otherwise it will stop the script (the first time you run it, there isn't an output file yet..).
Oh, of course, you are right)
Thanks again... I really should have tested thoroughly every time before commit and push. Nice to have you guys helping.
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
The project is at https://github.com/johnathan79717/codeforces-parser
You can run
git clone https://github.com/johnathan79717/codeforces-parser.git
or there's a button at the bottom right corner of the page that allows you to download a zip file.We haven't try it on windows. You're welcome to try it out and make a windows version.
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?
Hi, could you elaborate in what situation do you need proxy authentication? Thanks. This may work, but I haven't tried it yet.
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
I think it would be fantastic if Codeforces provides an API to obtain the test cases to not have to parse the entire HTML.
What is the license of your code? I have my own tester and I would like to use your parser. :)
You can use it however you want :)
This license is called WTFPL, just for you to know.
WAGL: What A Great License.
Ok, Thanks!
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!
Good Job!
Thanks a lot johnathan79717 and brunoja.
I'm using it with vim to improve workflow.
And following functions in .vimrc (vim configuration file)
And little changes in parser.py, if you like.
please tell how it will detect the division no.
Each division has a different contest ID which you can get from the contest URL. For example, Round #666 Div. 1 is https://codeforces.me/contest/1396 so the ID is 1396 and Round #666 Div. 2 is https://codeforces.me/contest/1397 so the ID is 1397.