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):
S = input()
Q = int(input())
query_list = []
for i in range(Q):
    query_list.append(input())


M = 10 ** 6
start_position = int(M / 2)
left_position = start_position
right_position = start_position + len(S)
result = ["-"] * M
for i in range(len(S)):
    # print(i + left_position)
    result[i + left_position] = S[i]

# result = "".join(result[left_position:right_position])
# print(result)
# exit(0)
reverse_flag = False

for i in range(Q):
    # print(i + 1)
    # print(result[left_position:right_position])
    q = query_list[i]
    # print(q)
    t = int(q[0])
    if t == 1:
        reverse_flag = not reverse_flag
    else:
        t, f, c = q.split()
        f = int(f)
        c = c
        if f == 1:
            if reverse_flag:
                result[right_position] = c
                right_position += 1
            else:
                result[left_position - 1] = c
                left_position -= 1
        else:
            if reverse_flag:
                result[left_position - 1] = c
                left_position -= 1
            else:
                result[right_position] = c
                right_position += 1
    # print(t, f, c, result)
    # print("--------")
    print(result[left_position:right_position])
# result = str(result)
# result = "".join(result)
# print(result)
# print(result[left_position:right_position])

result = "".join(result[left_position:right_position])
if reverse_flag:
    result = result[::-1]
print(result)

S = input()
Q = int(input())
query_list = []
for i in range(Q):
    query_list.append(input())


M = 10 ** 6
start_position = int(M / 2)
left_position = start_position
right_position = start_position + len(S)
result = ["-"] * M
for i in range(len(S)):
    # print(i + left_position)
    result[i + left_position] = S[i]

# result = "".join(result[left_position:right_position])
# print(result)
# exit(0)
reverse_flag = False

for i in range(Q):
    # print(i + 1)
    # print(result[left_position:right_position])
    q = query_list[i]
    # print(q)
    t = int(q[0])
    if t == 1:
        reverse_flag = not reverse_flag
    else:
        t, f, c = q.split()
        f = int(f)
        c = c
        if f == 1:
            if reverse_flag:
                result[right_position] = c
                right_position += 1
            else:
                result[left_position - 1] = c
                left_position -= 1
        else:
            if reverse_flag:
                result[left_position - 1] = c
                left_position -= 1
            else:
                result[right_position] = c
                right_position += 1
    # print(t, f, c, result)
    # print("--------")
    #print(result[left_position:right_position])
# result = str(result)
# result = "".join(result)
# print(result)
# print(result[left_position:right_position])

result = "".join(result[left_position:right_position])
if reverse_flag:
    result = result[::-1]
print(result)

Verdicts: Runtime Error

Accepted