Documentation
Quick Start Guides

How to use generators

Introduction

In this guide we will see how to use a generator to produce the test cases for a problem. This is very useful for problems with many and/or larger tests.

The problem

In this guide we will use the following problem:

Create a program that reads N integers and print the sum. The input should be read from standard input, and conists of two lines. The first line is the number of integers N, and the second line the actual numbers separated by space.

Sample input:

8
7 2 4 -3 1 1 5 4

Files

To create an exercise using a generator, we will need the following files:

exercise.yml describes fundamental things about the exercise. solution.py contains the modal solution for the problem. generator.py is the program that generates test data, and the focus of this guide.

The exercise (exercise.yml)

Nothing special has to be done here to use a generator, but we need the file to create an exercise:

name: Sum
suggestedSolutionVisibility: afterSolved
description: Description of the sum problem...

The solution (solution.py)

We also need a solution as usual. This will be used to create the answer files.

N = int(input())
numbers = map(int, input().split())
print(sum(numbers))

The generator (generator.py)

This is the interesting file when it comes to generators. Let us first take a look at the final generator program:

import random

# Seed the random function to make the program deterministic
random.seed(42)

# Prints a correctly formatted test
def printTest(data):
    print("//@ Test")
    print(len(data))
    print(" ".join(map(str, data)))

# Generates a random test, and prints it
def genTest(n):
    seq = [random.randint(1,100000) for _ in range(n)]

    printTest(seq)

# Create sample test group
print("//@ TestGroup: Samples")
print("//@ Feedback: Full")
printTest([7, 2, 4, -3, 1, 1, 5, 4])
genTest(10)

# Create evaluation test group
print("//@ TestGroup: Tests")
print("//@ Feedback: OnlyResult")
printTest([1])
printTest([-5])

for s in [5, 100, 1000, 10000, 100000, 500000]:
    genTest(s)

When you run this program, it will print a file conforming to our "Single test file" [TODO link] format to standard output. Note how we seed the random function in order to make the program deterministic. Otherwise, we risk our input data will change when we upload the program again.

You can write your generator program in any language we support. If your generator program consists of multiple files, simply put all the files in the generator/ directory.

Upload to CodeJudge

You are now ready to upload the files to CodeJudge. This is done as any other file-based exercise (ie. using the sync feature). When you upload the files, the generator is executed, and as always you will be shown what changes have been made.

After having synchronized the exercise, you can go to the Edit tab of the exercise, and see the generated input/output.

Test it!

As a final step, it is a good idea to see if the exercise actually works as expected.