qyliang/CodeNanoFix huggingface dataset:
Buggy code:
from collections import deque
import sys
input = sys.stdin.readline
s = str(input().rstrip())
ans = deque(list(s))

f = True
q = int(input())
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        f = not f
    else:
        f_i = int(q[1])
        if f_i == 1 ^ f:
            ans.appendleft(q[2])
        else:
            ans.append(q[2])
ans = "".join(list(ans))
if not f:
    ans = ans[::-1]
print(ans)


Fixed code:
from collections import deque
import sys
input = sys.stdin.readline

s = str(input().rstrip())
ans = deque(list(s))

f = True
q = int(input())
for i in range(q):
    q = list(map(str, input().split()))
    if int(q[0]) == 1:
        if f:
            f = False
        else:
            f = True
    else:
        f_i = int(q[1])
        if f_i == 1:
            if f:
                ans.appendleft(q[2])
            else:
                ans.append(q[2])
        else:
            if f:
                ans.append(q[2])
            else:
                ans.appendleft(q[2])
ans = list(ans)
if f:
    print("".join(ans))
else:
    print("".join(ans[::-1]))
Buggy code:
from collections import deque
s = deque(input())
q = int(input())

flag = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": flag = 1 - flag
  else:
    if query[1] == "1":
      if flag == 0: s.appendleft(query[2])
      else: s.append(query[2])
    else:
      if flag == 0: s.append(query[2])
      else: s.appendleft(query[2])

if query[0] == "1": s.reverse()
print(*s, sep="")

Fixed code:
from collections import deque
s = deque(input())
q = int(input())

flag = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": flag = 1 - flag
  else:
    if query[1] == "1":
      if flag == 0: s.appendleft(query[2])
      else: s.append(query[2])
    else:
      if flag == 0: s.append(query[2])
      else: s.appendleft(query[2])

if flag == 1: s.reverse()
print(*s, sep="")
Buggy code:
s = input()
q = int(input())

f1 = 1
f2 = 2

st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        f1 = f2
        f2 = f1
    elif int(q[0]) == 2:
        if int(q[1]) == f1:
            st = q[2] + st
        elif int(q[1]) == f2:
            se += q[2]


ans = st + s + se
if f1 == 2:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
print(ans)

Fixed code:
s = input()
q = int(input())

f1 = 1
f2 = 2
cnt = 0
st = ""
se = ""

for _ in range(q):
    q = input().split()
    if int(q[0]) == 1:
        cnt += 1
    elif int(q[0]) == 2:
        if cnt % 2 == 0:
            if int(q[1]) == 1:
                st = q[2] + st
            elif int(q[1]) == 2:
                se += q[2]
        else:
            if int(q[1]) == 1:
                se += q[2]
            elif int(q[1]) == 2:
                st = q[2] + st


ans = st + s + se
if cnt % 2 == 0:
    print(ans)
else:
    ans = list(ans)
    ans.reverse()
    ans = "".join(ans)
    print(ans)
Buggy code:
from collections import deque
s = input()
q = int(input())

s = deque(s)
t = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": t = 1 - t
  else:
    if t == 0:
      if query[1] == "1": s.appendleft(query[2])
      elif query[1] == "2": s.append(query[2])
    elif t == 1:
      if query[1] == "1": s.append(query[2])
      elif query[1] == "2": s.appendleft(query[2])
if query[0] == "1":print("".join(s)[::-1])
else: print("".join(s))

Fixed code:
from collections import deque
s = input()
q = int(input())

s = deque(s)
t = 0
for _ in range(q):
  query = list(map(str, input().split()))
  if query[0] == "1": t = 1 - t
  else:
    if t == 0:
      if query[1] == "1": s.appendleft(query[2])
      elif query[1] == "2": s.append(query[2])
    elif t == 1:
      if query[1] == "1": s.append(query[2])
      elif query[1] == "2": s.appendleft(query[2])
if t == 1:print("".join(s)[::-1])
else: print("".join(s))
Buggy code:
S = input()
Q = int(input())
#query = [input().split() for _ in range(Q)]
rev = False
forw = ""
back = ""
for i in range(Q):
  query = input().split()
  if query[0] == "1":
    rev = not rev
  else:
    if (not rev and query[1] == "1") or (rev and query[1] == "2"):
      forw = query[2] + forw
    else:
      back += query[2]
S = forw + S + back
#print(S)
if rev:
  #print(S[::-1])
  for i in range(0,len(S), -1) :
    print(S[i], end="")
  #S = back[::-1] + S[::-1] + forw[::-1]
else:
  #S = forw + S + back
  print(S)

Fixed code:
S = input()
Q = int(input())
#query = [input().split() for _ in range(Q)]

rev = False
forw = ""
back = ""
for i in range(Q):
  query = input().split()
  if query[0] == "1":
    rev = not rev
  else:
    if (not rev and query[1] == "1") or (rev and query[1] == "2"):
      forw = query[2] + forw
    else:
      back += query[2]
#S = forw + S + back
if rev:
  S = back[::-1] + S[::-1] + forw[::-1]
else:
  S = forw + S + back
print(S)
Buggy code:
import sys
input = sys.stdin.readline

def sol():
    s=input().rstrip()
    q=int(input())
    x=0
    for i in range(q):
        l=[i for i in input().split()]
        print(l)
        if l[0]=="1":
            x=(x+1)%2
        elif l[1]=="1":
            if x==0: s=l[2]+s
            else: s=s+l[2]
        elif l[1]=="2":
            if x==0: s=s+l[2]
            else: s=l[2]+s
    print(s if x==0 else s[::-1])

if __name__=="__main__":
    sol()


Fixed code:
import sys
input = sys.stdin.readline

def sol():
    s=input().rstrip()
    q=int(input())
    x=0
    a,b="",""
    for i in range(q):
        l=tuple([i for i in input().split()])
        if l[0]=="1":
            x=(x+1)%2
        elif l[1]=="1":
            if x==0: a=l[2]+a
            else: b=b+l[2]
        elif l[1]=="2":
            if x==0: b=b+l[2]
            else: a=l[2]+a
    s=a+s+b
    print(s if x==0 else s[::-1])

if __name__=="__main__":
    sol()
Buggy code:
import sys

S = input()

Q = int(input())
flag=0
mae = ''
matsu = ''
for i in range(Q):
    r = sys.stdin.readline().strip('\n')
    if r == '1':
        flag = 1- flag
    else:
        ni, f, c = r.split()
        if f=='1':
            if flag == 0:
                mae += c
            else:
                matsu += c
        else:
            if flag == 0:
                matsu += c
            else:
                mae += c
    if flag ==0:
        print(mae[::-1]+S+matsu)
    else:
        print(matsu[::-1]+S+mae)

Fixed code:
import sys

S = input()

Q = int(input())
flag=0
mae = ''
matsu = ''

for i in range(Q):
    r = sys.stdin.readline().rstrip('\n')
    if r == '1':
        flag = 1 - flag
    else:
        ni, f, c = r.split()
        if f=='1':
            if flag == 0:
                mae += c
            else:
                matsu += c
        else:
            if flag == 0:
                matsu += c
            else:
                mae += c


if flag ==0:
    print(mae[::-1]+S+matsu)
else:
    print(matsu[::-1]+S[::-1]+mae)
Buggy code:
S = list(input())
Q = int(input())
QL = []

for _ in range(Q):
    QL.append(input())

inversed = False
invc = 0

prefix = []
suffix = []

for query in reversed(QL):
    # print("qi:", query)
    if query == '1':
        #1
        inversed = not inversed
        invc += 1
    
    else:
        # print("2:inv:", inversed)
        #2
        __, f, c = query.split()

        if f == '1':
            if inversed:
                suffix.append(c)
            else:
                prefix.append(c)
        else:
            if not inversed:
                suffix.append(c)
            else:
                prefix.append(c)

# print("prefix:", prefix)
# print("suffix:", suffix)

if inversed:
    S = list(reversed(S))

print("".join(prefix) + "".join(S) + "".join(suffix))



Fixed code:
S = list(input())
Q = int(input())
QL = []

for _ in range(Q):
    QL.append(input())

inversed = False
invc = 0

prefix = []
suffix = []

for query in reversed(QL):
    # print("qi:", query)
    if query == '1':
        #1
        inversed = not inversed
        invc += 1
    
    else:
        # print("2:inv:", inversed)
        #2
        __, f, c = query.split()

        if f == '1':
            if inversed:
                suffix.append(c)
            else:
                prefix.append(c)
        else:
            if not inversed:
                suffix.append(c)
            else:
                prefix.append(c)

# print("prefix:", prefix)
# print("suffix:", suffix)

if inversed:
    S = list(reversed(S))

print("".join(prefix) + "".join(S) + "".join(list(reversed(suffix))))
Buggy code:
S = input()
Q = int(input())
Query = [list(map(str,input().split())) for _ in range(Q)]
from collections import deque 
flag = True
f = ""
l = ""
print(Query)
for i in Query:
    if i[0]=="1":
        if flag:
            flag = False
        else:
            flag = True
    else:
        if (flag==True and i[1]=="1") or (flag==False and i[1]=="2"):
            f += i[2] 
        elif (flag==True and i[1]=="2") or (flag==False and i[1]=="1"):
            l += i[2]
S = f[::-1]+S+l
if flag:
    print(S)
else:
    print(S[::-1])

Fixed code:
S = input()
Q = int(input())
Query = [list(map(str,input().split())) for _ in range(Q)]
from collections import deque 
flag = True
f = ""
l = ""
#print(Query)
for i in Query:
    if i[0]=="1":
        if flag:
            flag = False
        else:
            flag = True
    else:
        if (flag==True and i[1]=="1") or (flag==False and i[1]=="2"):
            f += i[2] 
        elif (flag==True and i[1]=="2") or (flag==False and i[1]=="1"):
            l += i[2]
S = f[::-1]+S+l
if flag:
    print(S)
else:
    print(S[::-1])
Buggy code:
from collections import deque
import sys
input = lambda: sys.stdin.readline().rstrip() 
def main():
    moji=input()
    d=deque(moji)
    cycle=int(input())
    flont_or_back=0
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    d.appendleft(ci)
                else:
                    d.append(ci)
            else:
                if fi=="1":
                    d.append(ci)
                else:
                    d.appendleft(ci)
    if flont_or_back%2==0:
        print("".join(d))
    else:
        print("".join(d[::-1]))

if __name__ == '__main__':
    main() 
    


Fixed code:
from collections import deque
import sys
input = lambda: sys.stdin.readline().rstrip() 
def main():
    moji=input()
    d=deque(moji)
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    d.appendleft(ci)
                else:
                    d.append(ci)
            else:
                if fi=="1":
                    d.append(ci)
                else:
                    d.appendleft(ci)
    if flont_or_back%2==0:
        print("".join(d))
    else:
        d.reverse()
        print("".join(d))

if __name__ == '__main__':
    main()
Buggy code:
import sys
input = sys.stdin.readline.rstrip()
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont=ci+flont
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont=ci+flont
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main() 
  

Fixed code:
import sys
def input():
    return sys.stdin.readline().rstrip()

moji=input()
cycle=int(input())
flont_or_back=0
flont=""
back=""
for i in range(cycle):
    line=input()
    if line[0]=="1":
        flont_or_back+=1
    else:
        fi=line[2]
        ci=line[4]
        if flont_or_back%2==0:
            if fi=="1":
                flont=ci+flont
            else:
                back+=ci
        else:
            if fi=="1":
                back+=ci
            else:
                flont=ci+flont
moji=flont+moji+back
if flont_or_back%2==0:
    print(moji)
else:
    print(moji[::-1])
Buggy code:
import sys
input = sys.stdin.readline
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont=ci+flont
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont=ci+flont
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main() 
  

Fixed code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
def main():
    moji=input()
    cycle=int(input())
    flont_or_back=0
    flont=""
    back=""
    for i in range(cycle):
        line=input()
        if line[0]=="1":
            flont_or_back+=1
        else:
            fi=line[2]
            ci=line[4]
            if flont_or_back%2==0:
                if fi=="1":
                    flont=ci+flont
                else:
                    back+=ci
            else:
                if fi=="1":
                    back+=ci
                else:
                    flont=ci+flont
    moji=flont+moji+back
    if flont_or_back%2==0:
        print(moji)
    else:
        print(moji[::-1])
    

if __name__ == '__main__':
    main()
Buggy code:
S = input()
Q = int(input())

heads_min_idx = 0
tails_max_idx = 0
heads = {}
tails = {}
rev_flag = 1  # 1 or -
for q in range(Q):
    tmp_q = input()
    if tmp_q == '1':
        rev_flag *= -1
    else:
        _, f, c = tmp_q.split()
        if rev_flag == 1:
            if f == '1':
                heads[heads_min_idx - 1] = c
                heads_min_idx -= 1
            else:
                tails[tails_max_idx + 1] = c
                tails_max_idx += 1
        else:
            if f == '1':
                tails[tails_max_idx + 1] = c
                tails_max_idx += 1
            else:
                heads[heads_min_idx - 1] = c
                heads_min_idx -= 1

if rev_flag == 1:
    head_str = ''.join([s for _, s in sorted(heads.items(), key=lambda x: x[1])])
    tail_str = ''.join([s for _, s in sorted(tails.items(), key=lambda x: x[1])])
    print(head_str + S + tail_str)
else:
    head_str = ''.join([s for _, s in sorted(tails.items(), key=lambda x: x[1], reverse=True)])
    tail_str = ''.join([s for _, s in sorted(heads.items(), key=lambda x: x[1], reverse=True)])
    print(head_str + S[::-1] + tail_str)


Fixed code:
S = input()
Q = int(input())

heads_min_idx = 0
heads_max_idx = 0
tails_min_idx = 0
tails_max_idx = 0
heads = {}
tails = {}
rev_flag = 1  # 1 or -
for q in range(Q):
    tmp_q = input()
    if tmp_q == '1':
        rev_flag *= -1
    else:
        _, f, c = tmp_q.split()
        if rev_flag == 1:
            if f == '1':
                heads[heads_min_idx - 1] = c
                heads_min_idx -= 1
            else:
                tails[tails_max_idx + 1] = c
                tails_max_idx += 1
        else:
            if f == '1':
                tails[tails_max_idx + 1] = c
                tails_max_idx += 1
            else:
                heads[heads_min_idx - 1] = c
                heads_min_idx -= 1

if rev_flag == 1:
    head_str = ''.join([s for _, s in sorted(heads.items(), key=lambda x: x[0])])
    tail_str = ''.join([s for _, s in sorted(tails.items(), key=lambda x: x[0])])
    print(head_str + S + tail_str)
else:
    head_str = ''.join([s for _, s in sorted(tails.items(), key=lambda x: x[0], reverse=True)])
    tail_str = ''.join([s for _, s in sorted(heads.items(), key=lambda x: x[0], reverse=True)])
    print(head_str + S[::-1] + tail_str)
Buggy code:
from collections import deque

s = input()
c = int(input())

que = deque(s)

flipped = False
for i in range(c):
    query = input()
    
    if query[0] == '1':
        flipped = not flipped
    
    else:
        t, f, c = query.split()
        if (f == '1') ^ flipped:
            que.appendleft(c)
        
        else:
            que.append(c)

ans = ''.join(que)

print(ans)

Fixed code:
from collections import deque

s = input()
c = int(input())

que = deque(s)

flipped = False
for i in range(c):
    query = input()
    
    if query[0] == '1':
        flipped = not flipped
    
    else:
        t, f, c = query.split()
        if (f == '1') ^ flipped:
            que.appendleft(c)
        
        else:
            que.append(c)



ans = ''.join(que)

if flipped:
    ans = ans[::-1]

print(ans)
Buggy code:
ions

S = input()
Q = int(input())

rev = False
deque = collections.deque()
for c in S:
    deque.append(c)
for _ in range(Q):
    read = input()
    if read == '1':
        rev = not rev
    else:
        _, f, c = read.split()
        if not rev:
            if f == '1':
                deque.appendleft(c)
            else:
                deque.append(c)
        else:
            if f == '1':
                deque.append(c)
            else:
                deque.appendleft(c)

ret = ''.join(deque if not rev else reversed(deque))
print(ret)


Fixed code:


import collections

S = input()
Q = int(input())

rev = False
deque = collections.deque()
for c in S:
    deque.append(c)
for _ in range(Q):
    read = input()
    if read == '1':
        rev = not rev
    else:
        _, f, c = read.split()
        if not rev:
            if f == '1':
                deque.appendleft(c)
            else:
                deque.append(c)
        else:
            if f == '1':
                deque.append(c)
            else:
                deque.appendleft(c)

ret = ''.join(deque if not rev else reversed(deque))
print(ret)
Buggy code:
S = input()
Slist = [S[i] for i in range(len(S))]
Q = int(input())
Query = [list(input().split()) for i in range(Q)]
Qlen = len(Query)
reverse = False
back = []
head = []

for idx, value in enumerate(Query):
    if value[0] == "1":
        reverse ^= True
        head, back = back, head
    else:
        if value[1] == "1":
            back = back + [value[2]]
        else:
            head = head + [value[2]]
if reverse == False:
    print( "".join(head.reverse()) + "".join(Slist) + "".join(back) )

else:
    print( ''.join(reversed(head)) + "".join(Slist[::-1]) + "".join(back))

Fixed code:
S = input()
Slist = [S[i] for i in range(len(S))]
Q = int(input())
Query = [list(input().split()) for i in range(Q)]
Qlen = len(Query)
reverse = False
back = []
head = []

for idx, value in enumerate(Query):
    if value[0] == "1":
        reverse ^= True
        head, back = back, head
    else:
        if value[1] == "1":
            head += [value[2]]
        else:
            back += [value[2]]

if reverse == False:
    head.reverse()
    print("".join(head + Slist + back))
else:
    head.reverse()
    print("".join(head + Slist[::-1] + back))
Buggy code:
S = input()
Q = int(input())

direction = 1
head = []
tail = []
for i in range(Q):
    T = input()
    if T == '1':
        direction = direction * -1
    else:
        T = T.split()
        if T[1] == '1':
            if direction == 1:
                head.append(T[2])
            else:
                tail.append(T[2])
        else:
            if direction == 1:
                tail.append(T[2])
            else:
                head.append(T[2])

head = head[::(direction * -1)]
head.append(S)
head.extend(tail[::direction])
S = "".join(head)[::direction]

print(S)

Fixed code:
S = input()
Q = int(input())

direction = 1
head = []
tail = []
for i in range(Q):
    T = input()
    if T == '1':
        direction = direction * -1
    else:
        T = T.split()
        if T[1] == '1':
            if direction == 1:
                head.append(T[2])
            else:
                tail.append(T[2])
        else:
            if direction == 1:
                tail.append(T[2])
            else:
                head.append(T[2])

head = head[::(-1)]
head.append(S)
head.extend(tail)
S = "".join(head)[::direction]

print(S)
Buggy code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    S = input()
    Q = int(input())

    reverse = False
    header = []
    footer = []
    for _ in range(Q):
        query = input()
        if query[0] == '1':
            reverse = not reverse
        else:
            _, f, c = query.split()
            if reverse:
                if f == '1':
                    footer = [c] + footer
                else:
                    header.append(c)
            else:
                if f == '1':
                    header.append(c)
                else:
                    footer = [c] + footer
    if reverse:
        print("".join(footer + list(S)[::-1] + header))
    else:
        print("".join(header) + S + "".join(footer))

if __name__ == '__main__':
    main()

Fixed code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    S = input()
    Q = int(input())

    reverse = False
    header = [''] * Q
    footer = [''] * Q
    for i in range(Q):
        query = input()
        if query[0] == '1':
            reverse = not reverse
        else:
            _, f, c = query.split()
            if reverse:
                if f == '1':
                    footer[i] = c
                else:
                    header[i] = c
            else:
                if f == '1':
                    header[i] = c
                else:
                    footer[i] = c
    if reverse:
        print("".join(footer[::-1] + list(S)[::-1] + header))
    else:
        print("".join(header[::-1]) + S + "".join(footer))

if __name__ == '__main__':
    main()
Buggy code:
S = input()
Q = int(input())
Query = [input().split() for i in range(Q)]
countA,countB = 0,0
latter = []
former = []
for i in range(Q):
    if(Query[Q-i-1][0] == "1"):
        countA += 1
        countB += 1
    else:
        t = lambda x:1 if x=="1" else 0
        if((t(Query[Q-i-1][1])+countB)%2 == 1):
            former.append(Query[Q-i-1][2])
        else:
            latter.append(Query[Q-i-1][2])
print(former)
print(latter)
print(countA)

for i in former:
    print(i,end="")
if(countA%2 == 0):
    print(S,end="")
else:
    for j in range(len(S)):
        print(S[len(S)-j-1],end="")
for i in latter:
    print(i,end="")

Fixed code:
S = input()
Q = int(input())
Query = [input().split() for i in range(Q)]
countA,countB = 0,0
latter = []
former = []
for i in range(Q):
    if(Query[Q-i-1][0] == "1"):
        countA += 1
        countB += 1
    else:
        t = lambda x:1 if x=="1" else 0
        if((t(Query[Q-i-1][1])+countB)%2 == 1):
            former.append(Query[Q-i-1][2])
        else:
            latter.append(Query[Q-i-1][2])

for i in former:
    print(i,end="")
if(countA%2 == 0):
    print(S,end="")
else:
    for j in range(len(S)):
        print(S[len(S)-j-1],end="")
for i in range(len(latter)):
    print(latter[len(latter)-i-1],end="")