Problem: Takahashi has a string S consisting of lowercase English letters.  Starting with this string, he will produce a new one in the procedure given as follows.  The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:    * If T_i = 1: reverse the string S.    * If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.      * If F_i = 1 : Add C_i to the beginning of the string S.     * If F_i = 2 : Add C_i to the end of the string S.  Help Takahashi by finding the final string that results from the procedure.

Submission attempts (in order):
from sys import stdin
from collections import deque

d = deque()

S = stdin.readline().rstrip().split()
Q = [int(x) for x in stdin.readline().rstrip().split()]

d.append(S[0])
# print(d)
flag = 0b0

for i in range(Q[0]):
    s = input().split()

    if len(s) == 1:
        # print(len(d[0]))
        if len(d[0]) != 1:
            flag = ~flag
            # print(flag)
        
        # S=S[::-1]
        

    else:
        if int(s[1]) == 1:
            if flag == 0:
                d.appendleft(s[2])
            else:
                s[2] = s[2][::-1]
                # print(s[2])
                d.append(s[2])
                # print(d)
            
            # S.insert(0,s[2])

        else:
            if flag == 0:
                d.append(s[2])
            else:
                s[2] = s[2][::-1]
                # print(s[2])
                d.appendleft(s[2])
            
            # S.append(s[2])
# print(flag)
ans = "".join(d)
# print(ans)
if flag == -1:
    ans = ans[::-1]
    
    # print(d)

# print("".join(d))
print(ans)


from sys import stdin

S = stdin.readline().rstrip().split()
Q = [int(x) for x in stdin.readline().rstrip().split()]

for i in range(Q[0]):
  s=input().split()

  if len(s)==1:
      
      S=S[::-1]
      
  else:
      if int(s[1])==1:
          S.insert(0,s[2])
          

      else:
          S.append(s[2])
for i in range(len(S)):
  print(S[i],end="")

from sys import stdin
from collections import deque

d=deque()

S = stdin.readline().rstrip().split()
Q = [int(x) for x in stdin.readline().rstrip().split()]

d.append(S[0])
#print(d)
for i in range(Q[0]):
  s=input().split()

  if len(s)==1:

      #S=S[::-1]
      #print(d)
      d.reverse()
      #print(d)

  else:
      if int(s[1])==1:
          d.appendleft(s[2])
          #print(d)
          #S.insert(0,s[2])


      else:
          d.append(s[2])
          #print(d)
          #S.append(s[2])
for i in range(len(d)):
  print(d[i],end="")

from sys import stdin
from collections import deque

d = deque()

S = stdin.readline().rstrip().split()
Q = [int(x) for x in stdin.readline().rstrip().split()]

d.append(S[0])
# print(d)
flag = 0b0

for i in range(Q[0]):
    s = input().split()

    if len(s) == 1:
        # print(len(d[0]))
        if len(d) == 1 and len(d[0]) == 1:
            pass
        else:
            flag = ~flag

        #     if len(d[0])!=:
        
        # if len(d[0]) != 1:
        
            # print(flag)
        
        # S=S[::-1]
        

    else:
        if int(s[1]) == 1:
            if flag == 0:
                d.appendleft(s[2])
            else:
                s[2] = s[2][::-1]
                # print(s[2])
                d.append(s[2])
                # print(d)
            
            # S.insert(0,s[2])

        else:
            if flag == 0:
                d.append(s[2])
            else:
                s[2] = s[2][::-1]
                # print(s[2])
                d.appendleft(s[2])
            
            # S.append(s[2])
# print(flag)
ans = "".join(d)
# print(ans)
if flag == -1:
    ans = ans[::-1]
    
    # print(d)

# print("".join(d))
print(ans)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted