User Tools

Site Tools


tutorial:cp-local-testing

Competitive Programming - How to use samples for local testing

While doing the exercises on our competitive programming platform you may want to test your programs locally, before uploading them to the website. To do this you are provided with sample text files containing the input for your program. Smaller examples might be entered by hand using the terminal as stdin, but the more complex ones require a better solution.

This tutorial will shortly introduce different ways on how to direct the sample file's contents to your program as stdin input.

For feedback on or suggestions for this guide feel free to write an e-mail to s6kopapauni-bonn.

Redirecting on posix complient operating systems (Linux, macOS, ...)

To input the sample files while working on posix complient operating systems (Linux, macOS, …) or while using a terminal like cygwin on a Windows OS you can use this command to redirect the input. path/to/sample in these examples refers to wherever you saved the input files. myprogram is the name of the program you want to test.

For compiled languages (like c, rust), once you compiled the program, you can use the pipe operator to redirect the file's content:

./myprogram < path/to/sample

Under Windows ./myprogram is ./myprogram.exe.

For interpreted languages like Python (or Julia) you have to call the interpreter instead:

python myprogram.py < path/to/sample

For java programs you also have to call the java after compiling with javac:

java myprogram < path/to/sample

For more information:

Redirection while using Windows

To redirect your program's input while on a Windows OS you will need to use the Windows CMD. The command reamins almost the same:

myprogramm.exe < path\to\sample

Redirecting while using IDEs

On most IDEs you will be able to pass command line arguments to run your program with. Use the same commands as above, without the myprogram part as an argument in your IDE. The path to the test file has to be relative to the project directory or an absolute path.

Below you will find exampls for some IDE's.

Spyder for Python - Version 5.0.0 and above

Follow these steps to redirect sample input to your script:

  1. Go to Run and select Configuration per file
  2. in the dialog select Execute in an external system terminal
  3. tick Interact with the Python console after execution
  4. tick Command line options and enter < path\to\sample in the field

Known issue: the console might instantly close after completion, to verify your results you can add an infinite loop at the end of your script, which will keep the terminal open. Do not forget to remove that hodling loop before uploading your solution.

while True:
    hold = 1

PyCharm for Python - Community Version 2021.1.1

Follow these steps to redirect sample input to your script:

  1. Go to Run and select Edit Configuration…
  2. in the dialog tick Redirect input from
  3. select the file in the input field

Visual Studio 2019 for c++

Follow these steps to redirect sample input to your programm:

  1. under Debug select ProjectName Debug Properties
  2. in the Configuration Properties go to Debugging
  3. enter < path\to\sample in the Command Arguments field, $(ProjectDir) can be use to link to the projects directory

CLion for c and c++ - Version 2021.1.1

Follow these steps to redirect sample input to your programm:

  1. Go to Run and select Edit Configuration…
  2. in the dialog tick Redirect input from
  3. select the file in the input field

IntelliJ IDEA for java - Community Version 2021.1.1

Follow these steps to redirect sample input to your programm:

  1. go to Run and select Edit Configuration…
  2. click on Modify options and then click Redirect input
  3. select the sample file in the field Redirect input from
tutorial/cp-local-testing.txt · Last modified: 2022/04/12 15:41 by jonas.charfreitag

Page Tools