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):
import sys
s = str(sys.stdin.readline())
Q = int(sys.stdin.readline())

for _ in range(Q):
	qu = input()
	if qu == '1':
		s = s[::-1]
	else:
		_, fi, ci = qu.split()
		if fi == '1':
			s = ci + s
		if fi == '2':
			s += ci
print(s)

from collections import deque
import sys

def main():
    s = sys.stdin.readline().strip()
    qn = int(sys.stdin.readline().strip())
    qs = [input().split() for _ in range(qn)]

    cn = 0
    s = deque(s)

    for q in qs:
        if len(q) == 1:
            cn+=1
        else:
            if q[1] == '1':
                s.appendleft(q[2])
            else:
                s.append(q[2])

        if cn%2 == 1:
            s.reverse()
            print('%s' % ''.join(s))

if __name__ == "__main__":

    main()

import sys

def main():

    try:
        Ti = n = 0
        S = []
        S = list(map(str, input()))
        Q = map(int, input())
        while n < 6:
            Query = input().split()
            if len(Query) == 1:
                S[0], S[-1] = S[-1], S[0]

            if len(Query) == 3:
                Ti, Fi, Ci = Query
                if Ti == '2':
                    if Fi == '1':
                        S.insert(0, Ci)
                    if Fi == '2':
                        S.append(Ci)
            n+=1
        print(''.join(S))

    except KeyboardInterrupt:
        sys.exit(0)

if __name__ == '__main__':
    main()

n = 0
S = str(input())
Q = int(input())

while n < Q:
    Query = input().split()
    if len(Query) == 1:
        s = S[:S.find('_')]
        e = S[S.rfind('_')+1:]
        c = S[S.find('_')+1:S.rfind('_')]
        ecs = e + '_' +c + '_' +s
        S = ecs
    if len(Query) == 3:
        Ti, Fi, Ci = Query
        if Ti == '2':
            if Fi == '1':
                S = Ci + '_' + S
            if Fi == '2':
                S+='_'+Ci
    n+=1

import sys
input = sys.stdin.readline

s = str(input())
Q = int(input())

for _ in range(Q):
	qu = input()
	if qu == '1':
		s = s[::-1]
	else:
		_, fi, ci = qu.split()
		if fi == '1':
			s = ci + s
		if fi == '2':
			s += ci
print(s)

from collections import deque
import sys

def main():
    s = sys.stdin.readline().strip()
    qn = int(sys.stdin.readline().strip())
    qs = [input().split() for _ in range(qn)]

    s = deque(s)
    r = 1
    for q in qs:
        if len(q) == 1:
            r = r * -1

        else:
            if q[1] == '1':
                if r == 1:
                    s.appendleft(q[2])
                else:
                    s.append(q[2])
            else:
                if r == 1:
                    s.append(q[2])
                else:
                    s.appendleft(q[2])

    if r == 1:
        print(''.join(s))
    else:
        s.reverse()
        print(''.join(s))

if __name__ == "__main__":
    main()

Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Accepted