Problem p02692 (python)

Fixed submission:
# -*- coding: utf-8 -*-
abc = ["A","B","C"]

def calculate(x,y, q1,q2):
    flag = 0
    ans = ""
    if x == 1 and y == 1:
        if q1 == q2:
            y -= 1
            x += 1
            ans = abc[q1[0]]

        elif q2 != "":
            t = set(q1) & set(q2)
            s = set(q1) - t
            #print(q2)
            #print(int(list(t)[0]),int(list(s)[0]))
            if int(list(t)[0]) < int(list(s)[0]):
                y -= 1
                x += 1
                ans = abc[q1[0]]
            else:
                x -= 1
                y += 1
                ans = abc[q1[1]]
        else:
            y -= 1
            x += 1
            ans = abc[q1[0]]

    elif x <= y:
        y -= 1
        x += 1
        #print(y)
        if y < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[0]]
    else:
        x -= 1
        y += 1
        ans = ""
        #print(x)
        if x < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[1]]
    return (x, y, ans, flag)

def main():
    N, A, B, C  =  [int(i) for i in input().split()]
    s = []
    for i in range(N):
        t = input()
        if t == "AB":
            s.append([0,1])
        if t == "AC":
            s.append([0,2])
        if t == "BC":
            s.append([1,2])
    #print(s)
    answers  = []
    for i, q1 in enumerate(s):
        ans = ""
        f = 0
        q2 = ""
        if i+1 != N:
            q2 = s[i+1]

        if q1 == [0,1]:
            #print(1)
            A, B, ans, f = calculate(A,B,q1,q2)
        if q1 == [1,2]:
            #print(2)
            B, C, ans,f  = calculate(B,C,q1,q2)
        if q1 == [0,2]:
            #print(3)
            A, C, ans,f  = calculate(A,C,q1,q2)
        answers.append(ans)
        if f == 1:
            return
    print("Yes")

    for a in answers:
        print(a)
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
abc = ["A","B","C"]

def calculate(x,y, q1,q2):
    flag = 0
    ans = ""
    if x == 1 and y == 1:
        if q1 == q2:
            y -= 1
            x += 1
            ans = abc[q1[0]]

        elif q2 != "":
            t = set(q1) & set(q2)
            s = set(q1) - t
            "print(q2)
            #print(int(list(t)[0]),int(list(s)[0]))
            if int(list(t)[0]) < int(list(s)[0]):
                y -= 1
                x += 1
                ans = abc[q1[0]]
            else:
                x -= 1
                y += 1
                ans = abc[q1[1]]
        else:
            y -= 1
            x += 1
            ans = abc[q1[0]]

    elif x <= y:
        y -= 1
        x += 1
        #print(y)
        if y < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[0]]
    else:
        x -= 1
        y += 1
        ans = ""
        #print(x)
        if x < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[1]]
    return (x, y, ans, flag)

def main():
    N, A, B, C  =  [int(i) for i in input().split()]
    s = []
    for i in range(N):
        t = input()
        if t == "AB":
            s.append([0,1])
        if t == "AC":
            s.append([0,2])
        if t == "BC":
            s.append([1,2])
    #print(s)
    answers  = []
    for i, q1 in enumerate(s):
        ans = ""
        f = 0
        q2 = ""
        if i+1 != N:
            q2 = s[i+1]

        if q1 == [0,1]:
            #print(1)
            A, B, ans, f = calculate(A,B,q1,q2)
        if q1 == [1,2]:
            #print(2)
            B, C, ans,f  = calculate(B,C,q1,q2)
        if q1 == [0,2]:
            #print(3)
            A, C, ans,f  = calculate(A,C,q1,q2)
        answers.append(ans)
        if f == 1:
            return
    print("Yes")

    for a in answers:
        print(a)
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
abc = ["A","B","C"]

def calculate(x,y, q1,q2):
    flag = 0
    ans = ""
    if x == 1 and y == 1:
        if q1 == q2:
            y -= 1
            x += 1
            #print(y)
            if y < 0:
                print("No")
                flag = 1
                return(x, y, ans, flag)
            ans = abc[q1[0]]

        elif q2 != "":
            t = set(q1) & set(q2)
            s = set(q1) - t
            #print(int(list(t)[0]),int(list(s)[0]))
            if int(list(t)[0] < int(list(s)[0]):
                y -= 1
                x += 1
                ans = abc[q1[0]]
            else:
                x -= 1
                y += 1
                ans = abc[q1[1]]

    elif x <= y:
        y -= 1
        x += 1
        #print(y)
        if y < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[0]]
    else:
        x -= 1
        y += 1
        ans = ""
        #print(x)
        if x < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[1]]
    return (x, y, ans, flag)

def main():
    N, A, B, C  =  [int(i) for i in input().split()]
    s = []
    for i in range(N):
        t = input()
        if t == "AB":
            s.append([0,1])
        if t == "AC":
            s.append([0,2])
        if t == "BC":
            s.append([1,2])
    #print(s)
    answers  = []
    for i, q1 in enumerate(s):
        ans = ""
        f = 0
        q2 = ""
        if i+1 != N:
            q2 = s[i+1]

        if q1 == [0,1]:
            #print(1)
            A, B, ans, f = calculate(A,B,q1,q2)
        if q1 == [1,2]:
            #print(2)
            B, C, ans,f  = calculate(B,C,q1,q2)
        if q1 == [0,2]:
            #print(3)
            A, C, ans,f  = calculate(A,C,q1,q2)
        answers.append(ans)
        if f == 1:
            return
    print("Yes")

    for a in answers:
        print(a)
if __name__ == "__main__":
    main()