The background of the problem is surprisingly fun to read.
It's been a while since I've had a lot of professional competition, so I've been solving simple problems as a rehabilitation for the past few days. Since it is a good idea, I changed the environment of C ++ + Emacs that I used in the past to Python + PyCharm.
At that time, I have created a template that makes it easy to add tests (as it is), so I will post it here. Below is a description of the created template.
First of all, it is the iron rule of the competition professional to check the constraints.
This time, we will target Codeforces among the many competition pro sites. Codeforces has the following features:
First of all, since there are few test cases available, the key is how easy it is to add your own tests. (Conversely, there is little need to automatically generate test cases from problem statements.)
The number one feature is that it becomes a bottleneck at that time. When exchanging function arguments and return values (contests such as TopCoder), you can write unit tests normally, so it is easy to receive IDE support. However, standard input / output is not supported so much, and it seems difficult to test even with jUnit. Therefore, it is necessary to take a slightly troublesome method.
Also, if you submit the file as it is, it will be easier, so I want to make it so that it can be submitted as it is.
Fit into the mold.
The introduction has become long, but I will put the template I made here. There are 2 files, One is a file that you can write the code and submit as it is, The other is a file that describes the test.
First, the file that writes the code. Don't look! After (CV: Umez), you don't need to see it while solving the problem.
main.py
# -*- coding: utf-8 -*-
import sys
#Increase the required import
class Solver(object):
def run(self):
###List the answer here###
####################################
#Don't look from here!
###List the library here###
if __name__ == '__main__':
Solver().run()
Next, the file to write the test.
test_solver.py
# -*- coding: utf-8 -*-
from unittest import TestCase
data = [
(
"""
Input 1
""".strip()
,
"""
Answer 1
""".strip()
),
(
"""
Input 2
""".strip()
,
"""
Answer 2
""".strip()
),
]
########################################
#Don't look from here!
class TestSolver(TestCase):
def test_run(self):
import os
import sys
from StringIO import StringIO
import main
for i, testdata in enumerate(data):
print(str(i) + " test...")
saved_stdout = sys.stdout
try:
with open("temp_data.txt", "w+") as temp:
temp.write(testdata[0])
fd = os.open("temp_data.txt", os.O_RDONLY)
os.dup2(fd, sys.stdin.fileno())
out = StringIO()
sys.stdout = out
main.Solver().run()
output = out.getvalue().strip()
self.assertEquals(output, testdata[1])
finally:
sys.stdout = saved_stdout
Running TestSolver.test_run () in test_solver.py runs the test case group defined near the beginning of the file. In this function Receives standard input from a file Redirect standard output to variables doing. This allows you to replace standard input and standard output.
The test cases use here-documents, so you can easily add them by copying. No mouse training required.
Doesn't standard input need to go through a file? I thought, but for some reason I couldn't do it, so I gave up (a detailed person plz). Also, I purposely write the place that I could write using tempfile to the actual file, but Windows? In a minor OS like that, I get angry because I don't have permission to read tempfile. It's troublesome to avoid it ... I couldn't do it, so I gave up (a detailed person plz).
Almost as you can see. Basically, all you have to do is edit the area above the file.
The template is just a file, so you can use it in combination with your favorite editor. PyCharm is easy and nice as a comfortable coder. There is auto-completion, and it's easy to execute with shortcut keys. Of course, it's also a good idea to combine it with Emacs or Vim and fight with a familiar editor. There is no point in having Emacs coming first.
This will make it even more convenient! If you have the knowledge, please comment.
Recommended Posts