Problem:
Let _A, B, C, D, E_ be sets of integers and let _U_ is a universal set that includes all sets under consideration. All elements in any set are different (no repetitions).  u - **union** of two sets, _AuB_ = { _x ∈ U_ : _x ∈ A_ or _x ∈ B_} is the set of all elements which belong to _A_ or _B_.  i - **intersection** of two sets, _AiB_ = { _x ∈ U_ : _x ∈ A_ and _x ∈ B_} is the set of all elements which belong to both _A_ and _B_.  d - **difference** of two sets, _AdB_ = { _x ∈ U_ : _x ∈ A_, _x ∉ B_} is the set of those elements of _A_ which do not belong to _B_.  s - **symmetric difference** of two sets, _AsB_ = ( _AdB_ ) _u_ ( _BdA_ ) consists of those elements which belong to _A_ or _B_ but not to both.  c - **complement** of a set, _cA_ = { _x ∈ U_ : _x ∉ A_}, is set of elements which belong to _U_ but do not belong to _A_. Unary operator _c_ has higest precedence.  The universal set _U_ is defined as a union of all sets specified in data.  Your task is to determine the result of an expression, which includes sets, set operations and parenthesis (any number of parenthesis and any correct enclosure of parenthesis may take place).

Incorrect submission (verdict: Wrong Answer):
import sys

def rpn(str):
    r = []
    stack = []
    for i in range(0, len(str)):
        c = str[i]
        if c in "idsu":
            while len(stack) > 0:
                if stack[-1] in "idsuc":
                    a = stack.pop()
                    r.extend(a)
                else:
                    break
            stack.extend(c)
        elif c == "c":
            stack.extend(c)
        elif c == "(":
            stack.extend(c)
        elif c == ")":
            while len(stack) > 0:
                a = stack.pop()
                if a == "(":
                    break
                r.extend(a)
        else:
            r.extend(c)
    while len(stack) > 0:
        a = stack.pop()
        r.extend(a)
    return r

def intersect(a, b):
    r = []
    for e in a:
        if e in b:
            r.extend([e])
    return r

def union(a, b):
    r = list(set(a + b))
    return r

def diff(a, b):
    r = []
    for e in a:
        if e not in b:
            r.extend([e])
    return r

def universal(sets):
    r = []
    for v in sets.values():
        r.extend(v)
    r = list(set(r))
    return r

def calc(rpn, sets):
    stack = []
    U = universal(sets)
    for c in rpn:
        if c in "iuds":
            op2 = stack.pop()
            op1 = stack.pop()
            if c == "i":
                x = intersect(op1, op2)
                stack.append(x)
            elif c == "u":
                x = union(op1, op2)
                stack.append(x)
            elif c == "d":
                x = diff(op1, op2)
                stack.append(x)
            elif c == "s":
                x = diff(op1, op2)
                y = diff(op2, op1)
                z = union(x, y)
                stack.append(z)
        elif c == "c":
            op1 = stack.pop()
            x = diff(U, op1)
            stack.append(x)
        else:
            stack.append(sets[c])
    return stack.pop()
            
lno = 0
sets = {}
name = ""
for line in sys.stdin:
    lno += 1
    if lno % 2 == 1:
        name = line.strip().split()[0]
    elif name != "R":
        elem = list(map(int, line.strip().split()))
        sets[name] = elem
    else:
        e = rpn(line.strip())
        result = calc(e, sets)
        result.sort()
        for n in result:
            print(n, end = " ")
        print()


Fixed, accepted submission:
import sys

def rpn(str):
    r = []
    stack = []
    for i in range(0, len(str)):
        c = str[i]
        if c in "idsu":
            while len(stack) > 0:
                if stack[-1] in "idsuc":
                    a = stack.pop()
                    r.extend(a)
                else:
                    break
            stack.extend(c)
        elif c == "c":
            stack.extend(c)
        elif c == "(":
            stack.extend(c)
        elif c == ")":
            while len(stack) > 0:
                a = stack.pop()
                if a == "(":
                    break
                r.extend(a)
        else:
            r.extend(c)
    while len(stack) > 0:
        a = stack.pop()
        r.extend(a)
    return r

def intersect(a, b):
    r = []
    for e in a:
        if e in b:
            r.extend([e])
    return r

def union(a, b):
    r = list(set(a + b))
    return r

def diff(a, b):
    r = []
    for e in a:
        if e not in b:
            r.extend([e])
    return r

def universal(sets):
    r = []
    for v in sets.values():
        r.extend(v)
    r = list(set(r))
    return r

def calc(rpn, sets):
    stack = []
    U = universal(sets)
    for c in rpn:
        if c in "iuds":
            op2 = stack.pop()
            op1 = stack.pop()
            if c == "i":
                x = intersect(op1, op2)
                stack.append(x)
            elif c == "u":
                x = union(op1, op2)
                stack.append(x)
            elif c == "d":
                x = diff(op1, op2)
                stack.append(x)
            elif c == "s":
                x = diff(op1, op2)
                y = diff(op2, op1)
                z = union(x, y)
                stack.append(z)
        elif c == "c":
            op1 = stack.pop()
            x = diff(U, op1)
            stack.append(x)
        else:
            stack.append(sets[c])
    return stack.pop()
            
lno = 0
sets = {}
name = ""
for line in sys.stdin:
    lno += 1
    if lno % 2 == 1:
        name = line.strip().split()[0]
    elif name != "R":
        elem = list(map(int, line.strip().split()))
        sets[name] = elem
    else:
        e = rpn(line.strip())
        result = calc(e, sets)
        result.sort()
        if len(result) > 0:
            print(" ".join([str(n) for n in result]))
        else:
            print("NULL")
        sets = {}