Why am I getting WA? (POPGATES)

This is first time I am submitting a solution. My code works fine for input given as sample. But I am getting WA a the end. Posting my code and output for sample.

Problem Code: POPGATES

#code for persia
def code(li,k):

   for i in range(k):
          swap=0
          if(li[-1]=='H'):
                 swap=1
          li.pop()
          
          #rightmost
          if(swap==1):
                 for j in range(len(li)):
                        if(li[j]=='H'):
                               li[j]='T'
                        else:
                               li[j]='H'
   print(li.count('H'))

t=int(input())
for i in range(t):
li=[]
line=input()
line.split()
n=int(line[0])
k=int(line[2])
line = list(input())

   for j in range(len(line)):
          if(line[j]=='H' or line[j]=='T'):
                 li.append(line[j])
   print(li)     
   code(li,k)

Example Input

3
5 3
H T T H T
7 4
H H T T T H H
6 1
T H T H T T

Example Output

1
2
2

your indentation is messed up a bit. assuming that is not the case when you’re submitting.

make sure you’re not printing this in the submission.

k is line[1]

@dhana_son Here is my python solution for this: https://www.codechef.com/viewsolution/29982904

Thank you. But I am facing the same issue. I had taken line[2] as line[1] is a space.

Nice use of dictionary.

Appreciate your support. But I am trying to understand the way the solutions are checked. Because any solution whose input is taken this way is facing same fate.

The code executes perfectly with sample input. But submission gives Wrong answer.

What is the need of append (line[j])?

The list that is taken as input is having the spaces. So I am trying to remove them and adding only H or T to the final list.

@dhana_son If you try this you don’t need to do this.
a = map(int, input().split())
And then you can make list directly from this:
li = list(a)
Then if you give input like: H T T H T
Your output will be like: [‘H’, ‘T’, ‘T’, ‘H’, ‘T’], So you don’t have to remove spaces and recommend this.

But if you do this:
a = input()
li = list(a)
Then if you give input like: H T T H T
Your output will be like: [‘H’, ’ ', ‘T’, ’ ', ‘T’, ’ ', ‘H’, ’ ', ‘T’] and you have to remove spaces.

Guys a little help please
Can you please what is wrong with this code???
my testcases all passed but the task from codechef failed.

def count_H_T(arr):
    return arr.count('T'), arr.count('H')
if  __name__ == '__main__':
    testcases = int(input())
    for i in range(testcases):
        n, k = map(int, input().split())
        arr = input().split()

        T_count_1st_half, H_count_1st_half = count_H_T(arr[:(n-k)]) 
        T_count_2nd_half, H_count_2nd_half = count_H_T(arr[(n-k):])

        if H_count_2nd_half%2==0 or H_count_2nd_half==0:
            print(H_count_1st_half)
        else:
            print(T_count_1st_half)

I realise it’s been a while, but in my opinion there aren’t enough Python3 programmers here, so in case it helps anyone else…

It’s not sufficient to count the ‘H’ at the end of the line. Because, after you take the first ‘H’, all the ‘T’ in line will become ‘H’. As an example, consider the following input:

1
10 4
H H H H H H T T T H

Your code produces the answer ‘0’. The correct answer is ‘6’.

[apologies: edited because I used an illegal combination in my example]