14,933,180 members
See more:
This is the sample input...
3
5 1
1 0 0 0 0
5
5 1
1 0 0 0 2
4
5 2
2 0 0 0 1
3 1
This the actual output and I want my output in the same format...
4
1
-1 0
But...This is my output...:(
4
1
-1
0
and below is my code and I'm unable to find out the error.

What I have tried:

Python
```for _ in range(int(input())):
n , m = map(int,input().split())
N = list(map(int,input().split()))
M = list(map(int,input().split()))
t1 = [0]*n
t2 = [0]*n
#for array t1
for i in range(1,n):
if N[i] == 1:
pass
elif N[i] != 1 and N[i-1] == 1 and t1[i-1]>=0:
t1[i] = t1[i-1] + 1
elif N[i] != 1 and N[i-1] != 1 and t1[i-1]==0 :
t1[i] = -1
elif N[i] != 1 and N[i-1] != 1 and t1[i-1]>0:
t1[i] = t1[i - 1] + 1
elif N[i] != 1 and N[i-1] != 1 and t1[i-1]==-1:
t1[i] = -1
#for array t2
x = n-1
for i in range(x,0,-1):
if N[i] == 2:
pass
elif N[i] != 2 and i==x :
t2[i] = -1
elif N[i] != 2 and N[i+1]==2 :
t2[i] = t2[i+1] + 1
elif N[i] != 2 and t2[i+1]>0 and N[i+1]!=2:
t2[i] = t2[i+1] +1
elif N[i] !=2 and N[i+1] != 2 and t2[i+1]== -1:
t2[i] = -1
for i in range(len(M)):
y = M[i]-1
if t1[y]<0 and t2[y]<0 :
c = -1
elif t1[y]<0 and t2[y]>0:
c = (t2[y])
elif t1[y]>=0 and t2[y]<0:
c=(t1[y])
elif t1[y]>=0 and t2[y]>=0 and t1[y]>t2[y]:
c=(t2[y])
elif t1[y]>=0 and t2[y]>=0 and t1[y]<t2[y]:
c=(t1[y])
elif t1[y]>=0 and t2[y]>=0 and t1[y]==t2[y]:
c=(t1[y])
print(c)```
Posted
Updated 10-Jun-21 4:14am
DevParty 10-Jun-21 10:06am

Debug the code and see what is happening.
CPallini 11-Jun-21 1:57am

What should the code do with the input?
That is what are the requirements?

## Solution 1

Having no syntax errors does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
```int Double(int value)
{
return value * value;
}```

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
If you don;t know how to use a debugger, start here: pdb — The Python Debugger — Python 3.9.5 documentation[^]

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
Muskan Verma 2021 10-Jun-21 13:26pm

I'm sorry sir but this is not the answer I was expecting, I'm not saying that my output is wrong, I'm just saying that format of my output is not right. like output I want is that -1 and 0 should come in a single line cause the input I was taking for that is [3,1] and the rest should come as it is. Actually, I'm dealing with this problem. I request u to read my question once again and run that too with the same sample input and then u will be able to detect my problem.
OriginalGriff 10-Jun-21 14:02pm

Yes, and it's your code that causes that.
So the first thing to do is to use the debugger to follow through your code, looking at exactly what it is doing to find out where you need to make changes.
Muskan Verma 2021 11-Jun-21 2:16am

yes sir I have used a debugger too and I'm not able to correct it that's why I have posted it here
OriginalGriff 11-Jun-21 3:16am

And what does the debugger show you?

I'm not being deliberately difficult, but you wrote the code, you know what each bit is meant to do. So where does the debugger show that the execution differs from what you intended?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard Deeming 245 Maciej Los 155 OriginalGriff 105 Richard MacCutchan 105 SPASWIN 40
 OriginalGriff 4,525 Richard MacCutchan 2,225 Richard Deeming 1,530 Patrice T 1,280 CPallini 1,050

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900