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 collections import deque
s = deque(input())
n = int(input())
initial = "left"
for i in range(n):
    order = list(input().split())
    if len(order) == 3:
        if initial == "left":
            if int(order[1]) == 1:
                s.appendleft(order[2])
            else:
                s.append(order[2])
        else:
            if int(order[1]) == 1:
                s.append(order[2])
            else:
                s.appendleft(order[2])
    else:
        initial = "right"
    
if initial == "left":
    print(''.join(s))
else:
    print(''.join(s)[::-1])

from collections import deque
s = deque()
n = int(input())
initial = "left"
for i in range(n):
    order = list(input().split())
    if len(order) == 3:
        if initial == "left":
            if int(order[1]) == 1:
                s.appendleft(order[2])
            else:
                s.append(order[2])
        else:
            if int(order[1]) == 1:
                s.append(order[2])
            else:
                s.appendleft(order[2])
    else:
        initial = "right"
    
if initial == "left":
    print(s)
else:
    print(s[::-1])

s = input()
n = int(input())
initial = "right"
for i in range(n):
    order = list(input().split())
    if len(order) == 3:
        if initial == "right":
            if int(order[1]) == 1:
                s = order[2] + s
            else:
                s += order[2]
        else:
            if int(order[1]) == 1:
                s += order[2]
            else:
                s = order[2] + s
    else:
        initial = "left"
    
if initial == "right":
    print(s)
else:
    print(s[::-1])

s = deque()
n = int(input())
initial = "left"
for i in range(n):
    order = list(input().split())
    if len(order) == 3:
        if initial == "left":
            if int(order[1]) == 1:
                s.appendleft(order[2])
            else:
                s.append(order[2])
        else:
            if int(order[1]) == 1:
                s.append(order[2])
            else:
                s.appendleft(order[2])
    else:
        initial = "right"
    
if initial == "left":
    print(s)
else:
    print(s[::-1])

from collections import deque
s = deque(input())
n = int(input())
initial = True
for i in range(n):
    order = list(input().split())
    if len(order) == 3:
        if initial:
            if int(order[1]) == 1:
                s.appendleft(order[2])
            else:
                s.append(order[2])
        else:
            if int(order[1]) == 1:
                s.append(order[2])
            else:
                s.appendleft(order[2])
    else:
        initial = not initial
    
if initial:
    print(''.join(s))
else:
    print(''.join(s)[::-1])

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Accepted