Skip to content

Validator

The validator is used for validating inputs, i.e. making sure they conform to the restrictions in the task statement.

Validator type

There are currently 2 validator types available:

Simple-42

The simple-42 validator is run as follows:

./validate <test_num> < input
Where test_num is the number of the test to which the input belongs. If the input is valid, the validator should exit with return code 42. Otherwise, it should exit with any other return code.

Example simple-42 validator

For a task of printing N positive integers that sum up to K, the validator may look like this:

#!/usr/bin/env python3
import sys

test = int(sys.argv[1])
n, k = map(int, input().split(" "))  # We use explicitly split(" ") to be more strict

assert 1 <= n <= 10**5, f"{n=} limits"
assert 1 <= k <= 10**9, f"{k=} limits"

if test == 1:
    assert n == k, "N and K should be equal"
elif test == 2:
    assert k <= 10**5, f"{k=} is too big"

try:
    input()
except EOFError:
    exit(42)  # The input is valid

assert False, "The input doesn't end when it should"

Simple-0

The simple-0 validator is run as follows:

./validate <test_num> < input
Where test_num is the number of the test to which the input belongs. If the input is valid, the validator should exit with return code 0. Otherwise, it should exit with any other return code.

This validator_type is not recommended

Return with exit code 0 can be sometimes caused by libraries. It is better to avoid this pitfall and use the simple-42 validator type instead.