What to do if the code doesn’t work? (original) (raw)

Last Updated : 23 Jul, 2025

This article will discuss the different routes one could take if their code fails to work in various scenarios.

If one has, seemingly at least, coded everything perfectly, tested solutions in the test cases included in the statements, but after the code's submission into the system it fails in the test and there are no explicit error codes to help identify and solve the problem. This problem could be caused by a vast number of reasons but it is usually one of the following:

Although the approach may differ based on the reason, generally one can follow the steps below:

**Measure time for small tests, medium tests, and large tests. One of the following possible outcomes can be encountered:

**Generate different tests and run your program against them until it crashes:

**How to generate tests: The simplest way to generate a test is to write a program that prints a test to a text file. Below is an example to illustrate the same:

**Program 1: Generating a test for the maximum pairwise distinct:

Python3 `

import sys n = int(sys.argv[1]) print(n) print(' '.join([str(i∗2)for i in range(n)])

`

The most cryptic thing here is probably sys.argv[1]. This is a getter for the first command-line argument. Now, how to use this to run the program? Copy the executable file or a python script to the same directory as the generating script and run the following commands:

_python script.py 17 > input.txt
_./your_program_name < input.txt

The use of Python3 is recommended over Python. It could help solve the issues in a few cases. Therefore, some tests can be automatically generated and the program can be run against them, but the following questions remain unanswered:

**Generating random tests and running your program on them: The following technique consisting of 3 parts can be used:

**Program 2: Generator that accepts a seed from the command line:

Python3 `

import random import sys

Input the number N

n = int(sys.argv[1]) myseed = int(sys.argv[2]) random.seed(myseed)

print(n)

1000 could also be moved to

parameters instead of making it

a hard constant in the code

print(' '.join([str(random.randint(1, 1000)) for i in range(n)])

`

**Program 3: The actual script:

Python3 `

import random import sys import os

Accept the number of tests as a

command line parameter

tests = int(sys.argv[1])

Accept the parameter for the

tests as a command line parameter

n = int(sys.argv[2]) for i in range(tests): print("Test #" + str(i))

# Run the generator gen.py with
# parameter n and the seed i

os.system("python3 gen.py " + str(n) + " " + str(i) + " > input.txt")

Run the model solution model.py

Notice that it is not necessary

that solution is implemented in

python, you can as well run

./model < input.txt > model.txt

for a C++ solution.

os.system("python3 model.py < input.txt > model.txt")

Run the main solution

os.system("python3 main.py < input.txt > main.txt")

Read the output of the

model solution

with open('model.txt') as f: model = f.read() print("Model: ", model)

Read the output of the

main solution :

with open('main.txt') as f: main = f.read() print("Main: ", main) if model != main: break

`

**How to add assertions: In Java, Python and C++ assert expression, assert expression, and assert(expression); respectively, will produce a runtime error if the Boolean expression is false. One possible usage of assertions is verifying that the answer to the program is consistent. Another common use case is to verify that the intermediate steps of the program are consistent.