March CookOff ProblemCode:GIFTSRC

can anyone tell me why my code is wrong despite it runs on the given input test cases.

###########################
t = int(input())
alpha=[]
for i in range(t):
start = [0, 0]
n = int(input())
s = input()
s1 = list(s)

if s1[0] == 'L':
    start[0] = int(start[0]) - 1
elif s1[0] == 'R':
    start[0] = int(start[0]) + 11
elif s1[0] == 'U':
    start[1] = int(start[1]) + 1
elif s1[0] == 'D':
    start[1] = int(start[1]) - 1
for j in range(1, len(s1)):
    if s1[j] == 'L' and s1[j - 1] != 'L' and s1[j - 1] != 'R':
        start[0] = int(start[0]) - 1
    elif s1[j] == 'R' and s1[j - 1] != 'L' and s1[j - 1] != 'R':
        start[0] = int(start[0]) + 1
    elif s1[j] == 'U' and s1[j - 1] != 'U' and s1[j - 1] != 'D':
        start[1] = int(start[1]) + 1
    elif s1[j] == 'D' and s1[j - 1] != 'U' and s1[j - 1] != 'D':
        start[1] = int(start[1]) - 1
    else:
        continue
alpha.append(start)

for beta in alpha:
print(tuple(beta))
##############################

your program did this:
for input:

3
5
LLLUR
6
LRLULR
3
LRD

Output for your program:

(0,1)

So, your output is wrong.
You print the output in tuple formation which is wrong. If you use tuple function for your output, it would be in this ‘()’ bracket, like your output (0,1) which is wrong. Your output for this above input should be:

0 1
-2 1
-1 -1

I think it will help to understand where are you wrong.
Here is my program:

t = int(input())
for i in range(t):
    n = int(input())
    s = input()
    li1 = list(s)
    k = 1
    a,b=0,0
    if li1[0] == 'L':
        a -= 1
    elif li1[0] == 'R':
        a += 1
    elif li1[0] == 'U':
        b += 1
    elif li1[0] == 'D':
        b -= 1
    for i in range(len(li1)-1):
        if (li1[k] == 'L' and li1[k-1] != 'L' and li1[k-1] != 'R'):
            a -= 1
        elif (li1[k] == 'R' and li1[k-1] != 'L' and li1[k-1] != 'R'):
            a += 1
        elif (li1[k] == 'U' and li1[k-1] != 'U' and li1[k-1] != 'D'):
            b += 1
        elif (li1[k] == 'D' and li1[k-1] != 'U' and li1[k-1] != 'D'):
            b -= 1
        k = k + 1
    print(a,b)
        

what is the error in this code then :slight_smile:

t = int(input())
alpha=[]
for i in range(t):
start = [0, 0]
n = int(input())
s = input()
s1 = list(s)

if s1[0] == 'L':
    start[0] = int(start[0]) - 1
elif s1[0] == 'R':
    start[0] = int(start[0]) + 11
elif s1[0] == 'U':
    start[1] = int(start[1]) + 1
elif s1[0] == 'D':
    start[1] = int(start[1]) - 1
for j in range(1, len(s1)):
    if s1[j] == 'L' and s1[j - 1] != 'L' and s1[j - 1] != 'R':
        start[0] = int(start[0]) - 1
    elif s1[j] == 'R' and s1[j - 1] != 'L' and s1[j - 1] != 'R':
        start[0] = int(start[0]) + 1
    elif s1[j] == 'U' and s1[j - 1] != 'U' and s1[j - 1] != 'D':
        start[1] = int(start[1]) + 1
    elif s1[j] == 'D' and s1[j - 1] != 'U' and s1[j - 1] != 'D':
        start[1] = int(start[1]) - 1
    else:
        continue
alpha.append(start)

for beta in alpha:
for beta1 in beta:
print(beta1,end=’ ')
print()

I think i told you there. your code do not print the expected ouput.
So, it get WA.

Read my previous post with full concentrate. I clearly said it there.

yeah i understood your point but the later code is executing the desired result as i have now changed my output format as per the question demanded.

If you get wrong answer again, then the problem of your code might be the second line. Your code still not give the proper output:
Input:

3
5
LLLUR
6
LRLULR
3
LRD

Output for your code:

0 1

expected output:

0 1
-2 1
-1 -1

I thing your code doesn’t follow the testcases. It just print output for the first testcases.

i ran the code in an offline ide and it shows the result.

Then you should try your code when the this question will add to practice session.

ok.btw thanks for replying to my query

Welcome. :slightly_smiling_face:

you don’t have to store the result of every query(case). you can just print that right away on the output

you have to print the output after each test case , as if after any test case online judge didn’t get the desired output or no output then it will give WA verdict.

t = int(input())

for i in range(t):
start = [0, 0]
n = int(input())
s = input()
s1 = list(s)

if s1[0] == 'L':
    start[0] = int(start[0]) - 1
elif s1[0] == 'R':
    start[0] = int(start[0]) + 11
elif s1[0] == 'U':
    start[1] = int(start[1]) + 1
elif s1[0] == 'D':
    start[1] = int(start[1]) - 1
for j in range(1, len(s1)):
    if s1[j] == 'L' and s1[j - 1] != 'L' and s1[j - 1] != 'R':
        start[0] = int(start[0]) - 1
    elif s1[j] == 'R' and s1[j - 1] != 'L' and s1[j - 1] != 'R':
        start[0] = int(start[0]) + 1
    elif s1[j] == 'U' and s1[j - 1] != 'U' and s1[j - 1] != 'D':
        start[1] = int(start[1]) + 1
    elif s1[j] == 'D' and s1[j - 1] != 'U' and s1[j - 1] != 'D':
        start[1] = int(start[1]) - 1
    else:
        continue
for j in start:
    print(j,end=' ')
print()

In this case even though i am printing my output after each case then also the code is not being accepted.

in elif , you have added 11 instead of 1

1 Like

In the above @anon46113952 block you have to mention 1 instead 11, after modifying this your code got AC : CodeChef: Practical coding for everyone

Will you please refer to the following simple code : CodeChef: Practical coding for everyone , your code is little complex

for _ in range(int(input())):
    n = int(input()); s = input()
    if s[0]=='L':
        x = -1; y = 0
    elif s[0]=='R':
        x = 1; y = 0
    elif s[0]=='U':
        x = 0; y = 1
    else:
        x = 0; y = -1
    for i in range(1,n):
        if s[i]==s[i-1]:
            continue
        elif s[i]=='L':
            if s[i-1]!='R':
                x -= 1
        elif s[i]=='R':
            if s[i-1] != 'L':
                x += 1
        elif s[i]=='U':
            if s[i-1]!='D':
                y += 1
        elif s[i]=='D':
            if s[i-1]!='U':
                y -= 1
    print(x,y)

Thanks bro for your help.:slight_smile: