Best Practices for STEM Loyola Programming Challenges
Contents:
- Overview
- Filename Convention
- Redirect Standard Input/Output for Testing
- Redefine Standard Input/Output for Testing
This tutorial assumes that you have already read Part 1: “Understand the Basics”.
Overview
Time is critical during a programming contest. Few seconds can be the difference between being the first or second. One of the time consuming parts during a programming challenge is repeatedly testing your code against the provided test cases. This tutorial will describe few tricks that you can use to test and upload your solution fast.
“…a professional is someone who can do his best work when he doesn’t feel like it“
Alistair Cooke
Filename Convention
The contest platform can help you populate most of the fields when you submit your solution. This happens when you name the file of your source code using the short code given for a particular problem.
The short code of problems is available in the title row where the color of a problem and its points are displayed. Usually, the short codes are A, B, C, D, and E.
If you are solving Problem A in C++, then naming your source code as A.cpp
will automatically cause the website to select the problem and the your programming language after you browse for the source file. Few seconds saved there.
Redirect Standard Input/Output for Testing
When you write a solution to a problem, you will need to test it several times for correctness. This is a tedious task, especially when the test data is huge. Fortunately, feeding data to your program for testing can and should be automated.
The first method does not involve modifying your source code and it involves the use of a Terminal or PowerShell.
During an ongoing challenge, if you visit the Problem Set section, you can download the sample test cases. Click on the “zip with all samples” to download all sample test cases. After extracting, each sample test case will have its input and output file (e.g. 1.in
and 1.out
for test cases #1). Copy and paste the test cases into the same folder that has your program.
Open a Terminal (In Linux/MAC) or PowerShell (in Windows) and navigate to the folder containing your program using a change directory command (cd). In Windows, suppose your program is in “C:\Users\STEM\Documents\My Programs”, then you will type:
cd "C:\Users\STEM\Documents\My Programs"
In Linux/MAC, if your program is in “/home/stem/Documents/Programs”, then you will type:
cd /home/stem/Documents/Programs
At this point, if you list the contents of your directory (using ls
command), you should be able to see your compiled program and the test cases.
By default, when you execute your program, the standard input is mapped to the keyboard. You will have to manually type in the test case data to check your program. However, you can map the standard input to the test case you want to test your program with. Suppose you are in Windows, and your program is called “A.exe”, and you want to run it using the data in “1.in”, you can type the following in the PowerShell.
cat 1.in | .\A.exe
Or you can also use the following command.
Get-Content 1.in | .\A.exe
Your program (A.exe) will be executed using the data in “1.in”. Results will be displayed in the PowerShell. Then, you can compare the results of your program with the sample test case one results in “1.out”. If you want to save the results of your program in a file (like “my-results.txt”), then you can add ” > my-results.txt” to either of the above commands.
cat 1.in | .\A.exe > my-results.txt
Adding ” > my-results.txt” redirects the standard output from being the PowerShell (screen) to the “my-results.txt” file.
Similarly, if you are using Linux/MAC, you can execute the above commands like:
cat 1.in | ./A > my-results.txt
You can omit the ” > my-results.txt” if you want the results to be displayed in the Terminal.
Redefine Standard Input/Output for Testing
This is another method of mapping the standard input/output to a file. This method involves modifying your source code but it is more convenient if you are using an IDE like CodeBlocks (Our recommended IDE for C/C++). We will demonstrate this method using C++.
This method uses the C function freopen
found in the cstdio
(or stdio.h
) header. Hence, you need to include this header like:
#include<cstdio>
Then, at the top of your main()
function, add the following line to use test data from “1.in” file.
freopen("1.in", "r", stdin);
When you compile and run your program (F9 in CodeBlocks), there will be no entering data manually as that will automatically be fetched from “1.in”. You are welcome!
At this point, the results of your program will be displayed on the screen. If you want to save them into a file, you can redefine the standard output to that file (say my-results.txt) using:
freopen("my-results.txt", "w", stdout);
A complete program that adds two numbers (taken from “1.in”) and displays results to the screen can be written as follows.
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
freopen("1.in", "r", stdin);
//freopen("my-results.txt", "w", stdout);
int A, B;
cin >> A >> B;
cout << A + B << endl;
return 0;
}
Code language: PHP (php)
You can comment out line 8 to have the results saved into “my-results.txt”.
Important Note: Remember to remove (or comment out) the lines that redefine the standard input/output (lines 7 and 8 in the above source code) before uploading your solution to the contest platform.
Using Java, the above can be written as follows:
import java.util.*;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintStream;
public class A {
public static void main( final String[] args) {
try {
System.setIn(new FileInputStream(new File("C:\\Users\\stem\\Documents\\My Programs\\1.in")));
//System.setOut(new PrintStream(new File("C:\\Users\\stem\\Documents\\My Programs\\my-results.txt")));
} catch (FileNotFoundException e) {
System.out.print(e.getMessage());
}
Scanner input = new Scanner(System.in);
int A = input.nextInt();
int B = input.nextInt();
System.out.println(A + B);
}
}
Code language: JavaScript (javascript)
Important Note: Remember to remove (or comment out) the lines that redefine the standard input/output (lines 11 and 12 in the above source code) before uploading your solution to the contest platform.
Stay tuned for Part 3 of this series and all the best tackling the next challenge!
2 replies on “Best Practices Part 2: Speed Up Testing and Uploading”
Thanks alot, I have learnt something new 🙂
I got to admit, this very useful and saves time