There are a few small things that you can keep in mind while testing your code.
New programmers usually forget to add a new line, testing on ideone will reveal such errors easily.
Sometimes we forget to reinitialize variables,arrays after every testcase. So input same test case atleast twice and check if same correct output is obtained. Some people use a “solve()” function for each test case , so variables are reinitialized automatically.
Always keep the constraints in mind, and know the range of the datatype you are using. I prefer to use long long int most of the times. Though some of the questions require even higher range.for ex. 100! cannot be represented in any data type in C++.
Try your code against boundary cases, if possible. Always check against small values like 0,1,2,3.
In questions involving modular arithmetic (Ans%SomeMod), ensure that ans is not obtaining a negative value , use (ans + mod)%mod to check that.
Recently in a question in nov long (chefwords), i missed a case involving duplicate strings and wasted so many hours. So take care of “Duplicates”.
Keep Practicing, experience will teach you everything.
One of the way of testing is to generate random test cases that can be solved manually for solutions and test your code against those test cases to figure out the bug…
For Example these are some of the random cases generator for problems of March Long Contest in PYTH 2.7.x
### Test case generator for DEVCLASS
import random
task=[0,1,2]
case=["B","G"]
test=100
print test
for i in range(test):
a=''
print random.choice(task)
siz=random.randrange(1,15)
for j in range(siz):
a+=random.choice(case)
print a
#------------------------------------------------#
#------------------------------------------------#
### Random Test case generator for SINEWAVE
import random
#number of test cases = 50
T=50
def task1():
print T
for i in range(T):
a=""
s=random.randrange(0,12+1)
c=random.randrange(0,12+1)
k=random.randrange(1,25+1)
a+=str(s)+" "+str(c)+" "+str(k)
print a
def task2():
print T
for i in range(T):
a=""
s=random.randrange(0,50+1)
c=0
k=random.randrange(1,50+1)
a+=str(s)+" "+str(c)+" "+str(k)
print a
def task3():
print T
for i in range(T):
a=""
s=random.randrange(0,50+1)
c=random.randrange(0,50+1)
k=1
a+=str(s)+" "+str(c)+" "+str(k)
print a
def task4():
print T
for i in range(T):
a=""
s=random.randrange(0,50+1)
c=random.randrange(0,50+1)
k=random.randrange(1,100+1)
a+=str(s)+" "+str(c)+" "+str(k)
print a
def forTesting():
print T
for i in range(T):
a=""
s=random.randrange(0,10+1)
c=random.randrange(0,10+1)
k=random.randrange(1,10+1)
a+=str(s)+" "+str(c)+" "+str(k)
print a
forTesting()
Copy random test cases in a file named test with extension .in in same directory your source code is saved and test your code or you may use ideone or codechef IDE …
Believe me you need patience but this works for 70-80% of the time…
the biggest problem I’m having is the codechef runs the code, then says ‘wrong answer’, but no feedback as to what was wrong with the output from my program.
My program works for the presented inputs and meets (as I read the criteria) all the listed criteria.
Learn gdb from here and here (vedios from CS50 by harvard).It is a commandline runtime debugging tool. It supports many languges but the tutorials are specifically for c and can be extended to c++ easily. For other languages also you can find the tutorials.
It makes debuggig really faster and you dont have to change your program again and again for debugging,
in manually u can check all the possible inputs into it and can check out. i used this method to test my last work about kerala houseboat packages. also there are a lot of testing tools are available in the market.
}
This is my program for input and output processing but codechef is giving me wrong answer. I even tested this answer on ideone and there it got successfully executed. so why codechef is giving me wrong answer??
We test our code in other sites and ide they work fine there, but when submit in codechef it simply says WRONG ANSWER. seriuosly? shouldn’t it clearly state whats went wrong. Its so irritating ti see that message again and again having no idea what did go wrong.