Google Code Jam is a programming competition, where you have to write a solution to a problem, that can then process a file containing sample input data. The competition is performed under time constraints, and therefore anything that can be written in advance is a help.
The coderunner module contains 2 classes FileHelper & CodeJamRunner.
The CodeJamRunner class is a helper for running specific solutions for a CodeJam problem. It handles all the standard code used to solve a problem:
- Reading in an input file
- Writing the data to the output file
- Checking test data, if the problem is being run in test mode.
The class exposes one method run(), the first 2 parameters are methods:
- data_builder: the method that should be used to build data structures from the input file. This method should return a single data object.
- solve_case: the method that should be used to solve a single input case (most problems have multiple input cases in a single input file). This method should return a string that is the solution for this case. Note: the CodeJamRunner automatically prepends the case number (as per the standard codejam solution format.
The final 2 parameters help construct the input file name. They are problem_name and problem_size (the default structure of an input file name is X-SIZE.input e.g. A-large.input). The CodeJamRunner, concatenates the 2 halves appropriately with the joining ‘-‘. So in this instance call the method with problem_name=’A’ and problem_size=’large’. Note: If the size parameter is omitted the runner will assume that it is a test case, and use the test data for this problem.
Code Jam input has a numnber of standard input formats. The FileHelper class simplifies the process of extracting the data in that format and putting it into the appropriate data structures. Example methods include:
- get_ints() – returns a list of ints
- get_grid() – returns a 2d list of floats
The data_builder has a FileHelper object passed as it’s first argument, with access to the appropriate input file.
An Example using the CodeJamRunner to solve a CodeJam Problem
For the input there is one line, containing 3 ints: height width and depth.
For the output, just return the volume of the object.
Clearly this is a very simplistic problem, but should provide an example of how to use the two classes in the module.
from codejam.utils.codejamrunner import CodeJamRunner class Dynam(object):pass def solver(data): """Calculate and return the volume of the cube using the data object.""" return data.h * data.w * data.d def data_builder(f): """Extract the data from the input file f, and return an object containing the data. The Dynam class is a regular class, and allows the properties to be stored on the object in dot notation. """ data = Dynam() data.h, data.w, data.d = f.get_ints() return data cjr = CodeJamRunner() # run the CodeJamRunner, with our data_builder and solver methods, for problem A. cjr.run(data_builder, solver, problem_name = "A")
If you would like to download the coderunner module source, it can be found on my github. Please note that it comes bundled with other CodeJam utilities that I have written – to be written about later.