Skip to content

Template

Adithya Joshua Dsilva edited this page Nov 17, 2020 · 2 revisions

Template Configuration

Templates are the building blocks which enable the use of some important functionalities like testing and submitting solutions to the online server. The configured template details are saved in the global configuration file, under the template key.

A template consists of several parts, which are as follows:

Alias

The alias is a unique title given to every created template, to differentiate it from the other configured templates. The alias name is what is passed to the command line flag --template.

The alias should be alpha numeric, without any whitespaces (must match regex [A-Za-z0-9]+). The alias name is case sensitive, and should be distinct from other alias names.

Template code

Template code is basically, the base template that you use. Of course, everyone has a template they use, even if its a 2 line code (it can even be empty if you really insist).

Save your template code in a file (ensure you don't move the file later, or you'll need to update the template configurations). This file is duplicated to a new file every time the generate command is used.

Templates also support variable placeholders, parsed using text/template package of go. Currently, the following generic placeholders are supported:

{{.date}} - Current date, in dd.mm.yyyy format
{{.time}} - Current time, in hh:mm format

A set of dynamic placeholders are provided from the local configuration file meta.yaml, if found. Here's a simple example, using generic and dynamic placeholders, and the resulting code file.

problem:
    name: Binary Table (Hard Version)
    memoryLimit: 256 megabytes
    inputStream: standard input
    outputStream: standard output
customFlag:
    addCredits: true
/*
Date: {{.date}} | Time: {{.time}}
{{if .customFlag.addCredits -}} Credits: cp-tools {{- end}}

{{if .problem.name -}} Problem: {{.problem.name}} {{- end}}
{{if .problem.timeLimit -}} Time limit: {{.problem.timeLimit}} {{- end}}
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
    // Enter code here...
    return 0;
}

With the above template and configuration, the generated file would then look as follows.

/*
Date: 18.11.2020 | Time: 13:48
Credits: cp-tools

Problem: Binary Table (Hard Version)

*/

#include<bits/stdc++.h>
using namespace std;

int main(){
    // Enter code here...
    return 0;
}

The example above is by no means exhaustive. Nevertheless, you may view the documentation of the official template library used, to make best use of the provided feature.

Test scripts

Test scripts are exclusively required by cpt test - the testing module, and specify the compilation and execution script in order to test your solution code.

Scripts also support generic placeholders, which are similarly evaluated at run time:

{{.file}}           - The solution file to test
{{.fileBasename}}   - The name of the file, without the extension

There are three scripts - prescript, runscript and postscript - executed before, for and after the testing, respectively.

  1. Prescript - This is executed exactly once, before testing. Run your compile scripts in this step, to compile the corresponding linked template. Interpreted languages like Python can omit this.

    Example script's to compile various language templates (in brackets) is given below.

    g++ -std=c++17 {{.file}}             (C++17)
    javac {{.file}}                      (Java)
    
  2. Runscript - This script is used to run your solution code, and is run once per test case.

    The input from the current sample test is passed to the process through standard input, and the solution output is taken from the standard output. Hence, do not pipe the sample tests in this script. Also, do not read or write data from any channel other than the standard input or standard output. Refer this for handling cases where the online judge requires you to use non-standard input/output.

    Given below is a list of various commands to run the solution code.

    python {{.file}}                     (Python)
    java {{.fileBasename}}               (Java)
    ./a.out                              (executable) -> linux
    a.exe                                (executable) -> windows
    
  3. Postscript - This script is executed after testing is completed, exactly once. Any residual executable, binary file cleanups that you would like to do, can be done at this step. Again, this is optional, and can be omitted if you don't require this functionality.

Language

This field differs from website to website, and is therefore configured on the corresponding website configuration file (instead of the global configuration, where the rest of the template is configured).

This specifies the language to select while submitting the solution (generated from this template) to the remote judge, on a particular website.

This is to be configured separately for each website, as shown below.

❯ cpt <website> config
? What configuration do you want to perform? template - set language
? Which template do you want to configure? <alias>
? Which language does template '<alias>' correspond to? <language>

A drop down will be presented at the third menu, allowing you to easily select the appropriate language, matching the template.

Clone this wiki locally