Problem:


Smart Calculator

Your task is to write a program which reads an expression and evaluates it.

The expression consists of numerical values, operators and parentheses, and the ends with '='.

The operators includes +, - , *, / where respectively represents, addition, subtraction, multiplication and division.

 Precedence of the operators is based on usual laws. That is one should perform all multiplication and division first, then addition and subtraction. When two operators have the same precedence, they are applied from left to right.

You may assume that there is no division by zero.

All calculation is performed as integers, and after the decimal point should be truncated

Length of the expression will not exceed 100.

-1 &times 109 ≤ intermediate results of computation ≤ 109

Input

The input is a sequence of datasets. The first line contains an integer n which represents the number of datasets. There will be n lines where each line contains an expression.

Output

For each datasets, prints the result of calculation.

Sample Input

2
4-2*3=
4*(8+4+3)=

Output for the Sample Input

-2
60



Sample input:
2
4-2*3=
4*(8+4+3)=


Sample output:
-2
60

Submission status: Runtime Error

Solution code:
from collections import deque

def eva(l, r, op):
    if op == "+":
        return l + r
    elif op == "-":
        return l - r
    elif op == "*":
        return l * r
    elif op == "/":
        return l // r


def calc(exp):
    i = 0
    num = deque()
    ops = deque()
    ope = ""
    while i < len(exp):
        if exp[i] == '(':
            end = exp[i:].rfind(")")
            num.append(calc(exp[i+1:i+end]))
            i += end + 1
            if ope:
                l = num.pop()
                r = num.pop()
                num.append(eva(l, r, ope))
                ope = ""
        if exp[i] in ["*", "/"]:
            ope = exp[i]
        elif exp[i] in ["+", "-"]:
            ops.append(exp[i])
        elif exp[i] == "=":
            pass
        else:
            temp = ""
            j = 0
            while i + j < len(exp) and exp[i+j].isnumeric():
                temp += exp[i+j]
                j += 1
            num.append(int(temp))
            i += j - 1
            if ope:
                num.append(eva(num.pop(), num.pop(), ope))
                ope = ""
        i += 1
    while len(ops) != 0:
        l = num.popleft()
        r = num.popleft()
        num.appendleft(eva(l,r,ops.popleft()))
    
    return num[0]


n = int(input())
for i in range(n):
    exp = input()
    print(calc(exp))

Submission status: Runtime Error

Solution code:
from collections import deque
from sys import stderr

def eva(l, r, op):
    if op == "+":
        return l + r
    elif op == "-":
        return l - r
    elif op == "*":
        return l * r
    elif op == "/":
        return l // r


def calc(exp):
    stderr.write(exp)
    i = 0
    num = deque()
    ops = deque()
    ope = ""
    while i < len(exp):
        if exp[i] == '(':
            end = exp[i:].rfind(")")
            num.append(calc(exp[i+1:i+end]))
            i += end + 1
            if ope:
                l = num.pop()
                r = num.pop()
                num.append(eva(l, r, ope))
                ope = ""
        if exp[i] in ["*", "/"]:
            ope = exp[i]
        elif exp[i] in ["+", "-"]:
            ops.append(exp[i])
        elif exp[i] == "=":
            pass
        else:
            temp = ""
            j = 0
            while i + j < len(exp) and exp[i+j].isnumeric():
                temp += exp[i+j]
                j += 1
            num.append(int(temp))
            i += j - 1
            if ope:
                num.append(eva(num.pop(), num.pop(), ope))
                ope = ""
        i += 1
    while len(ops) != 0:
        l = num.popleft()
        r = num.popleft()
        num.appendleft(eva(l,r,ops.popleft()))
    
    return num[0]


n = int(input())
for i in range(n):
    exp = input()
    print(calc(exp))

# N = int(input())
# for i in range(N):
#     print(int(eval(input().replace("/", "//").strip("="))))

Submission status: Runtime Error

Solution code:
from collections import deque

def eva(l, r, op):
    if op == "+":
        return l + r
    elif op == "-":
        return l - r
    elif op == "*":
        return l * r
    elif op == "/":
        return l // r


def calc(exp):
    i = 0
    num = deque()
    ops = deque()
    ope = ""
    while i < len(exp):
        if exp[i] == '(':
            end = 1
            s = 1
            while True:
                if exp[i+end] == "(":
                    s += 1
                elif exp[i+end] == ")":
                    s -= 1
                if s == 0:
                    break
                end += 1
            
            num.append(calc(exp[i+1:i+end]))
            i += end + 1
            if ope:
                l = num.pop()
                r = num.pop()
                num.append(eva(l, r, ope))
                ope = ""
        if exp[i] in ["*", "/"]:
            ope = exp[i]
        elif exp[i] in ["+", "-"]:
            ops.append(exp[i])
        elif exp[i] == "=":
            pass
        else:
            temp = ""
            j = 0
            while i + j < len(exp) and exp[i+j].isnumeric():
                temp += exp[i+j]
                j += 1
            num.append(int(temp))
            i += j - 1
            if ope:
                num.append(eva(num.pop(), num.pop(), ope))
                ope = ""
        i += 1
    while len(ops) != 0:
        l = num.popleft()
        r = num.popleft()
        num.appendleft(eva(l,r,ops.popleft()))
    
    return num[0]


n = int(input())
for i in range(n):
    exp = input()
    print(calc(exp))

Submission status: Runtime Error

Solution code:
from collections import deque

def eva(l, r, op):
    if op == "+":
        return l + r
    elif op == "-":
        return l - r
    elif op == "*":
        return l * r
    elif op == "/":
        return l // r


def calc(exp):
    i = 0
    num = deque()
    ops = deque()
    ope = ""
    while i < len(exp):
        if exp[i] == '(':
            end = 1
            s = 1
            while True:
                if exp[i+end] == "(":
                    s += 1
                elif exp[i+end] == ")":
                    s -= 1
                if s == 0:
                    break
                end += 1
            num.append(calc(exp[i+1:i+end]))
            i += end
            if ope:
                l = num.pop()
                r = num.pop()
                num.append(eva(l, r, ope))
                ope = ""
        if exp[i] in ["*", "/"]:
            ope = exp[i]
        elif exp[i] in ["+", "-"]:
            ops.append(exp[i])
        elif exp[i] == "=":
            pass
        else:
            temp = ""
            j = 0
            while i + j < len(exp) and exp[i+j].isnumeric():
                temp += exp[i+j]
                j += 1
            num.append(int(temp))
            i += j - 1
            if ope:
                num.append(eva(num.pop(), num.pop(), ope))
                ope = ""
        i += 1
    while len(ops) != 0:
        l = num.popleft()
        r = num.popleft()
        num.appendleft(eva(l,r,ops.popleft()))
    
    return num[0]


n = int(input())
for i in range(n):
    exp = input()
    print(calc(exp))

# N = int(input())
# for i in range(N):
#     print(int(eval(input().replace("/", "//").strip("="))))

Submission status: Runtime Error

Solution code:
from collections import deque
from sys import stderr

def eva(l, r, op):
    if op == "+":
        return l + r
    elif op == "-":
        return l - r
    elif op == "*":
        return l * r
    elif op == "/":
        return l // r


def calc(exp):
    stderr.write(exp)
    i = 0
    num = deque()
    ops = deque()
    ope = ""
    while i < len(exp):
        if exp[i] == '(':
            end = 1
            s = 1
            while True:
                if exp[i+end] == "(":
                    s += 1
                elif exp[i+end] == ")":
                    s -= 1
                if s == 0:
                    break
                end += 1
            num.append(calc(exp[i+1:i+end]))
            i += end
            if ope:
                l = num.pop()
                r = num.pop()
                num.append(eva(l, r, ope))
                ope = ""
        if exp[i] in ["*", "/"]:
            ope = exp[i]
        elif exp[i] in ["+", "-"]:
            ops.append(exp[i])
        elif exp[i] == "=":
            pass
        else:
            temp = ""
            j = 0
            while i + j < len(exp) and exp[i+j].isnumeric():
                temp += exp[i+j]
                j += 1
            num.append(int(temp))
            i += j - 1
            if ope:
                num.append(eva(num.pop(), num.pop(), ope))
                ope = ""
        i += 1
    while len(ops) != 0:
        l = num.popleft()
        r = num.popleft()
        num.appendleft(eva(l,r,ops.popleft()))
    
    return num[0]


n = int(input())
for i in range(n):
    exp = input()
    print(calc(exp))

Submission status: Runtime Error

Solution code:
from collections import deque
from sys import stderr

def eva(l, r, op):
    if op == "+":
        return l + r
    elif op == "-":
        return l - r
    elif op == "*":
        return l * r
    elif op == "/":
        return l // r


def calc(exp):
    i = 0
    num = deque()
    ops = deque()
    ope = ""
    while i < len(exp):
        if exp[i] == '(':
            end = 1
            s = 1
            while True:
                if exp[i+end] == "(":
                    s += 1
                elif exp[i+end] == ")":
                    s -= 1
                if s == 0:
                    break
                end += 1
            num.append(calc(exp[i+1:i+end]))
            i += end
            if ope:
                l = num.pop()
                r = num.pop()
                num.append(eva(l, r, ope))
                ope = ""
        if exp[i] in ["*", "/"]:
            ope = exp[i]
        elif exp[i] in ["+", "-"]:
            ops.append(exp[i])
        elif exp[i] == "=":
            pass
        else:
            temp = ""
            j = 0
            while i + j < len(exp) and exp[i+j].isnumeric():
                temp += exp[i+j]
                j += 1
            num.append(int(temp))
            i += j - 1
            if ope:
                num.append(eva(num.pop(), num.pop(), ope))
                ope = ""
        i += 1
    while len(ops) != 0:
        l = num.popleft()
        r = num.popleft()
        num.appendleft(eva(l,r,ops.popleft()))

    
    return num[0]


n = int(input())
for i in range(n):
    exp = input()
    print(calc(exp))

Submission status: Runtime Error

Solution code:
from collections import deque
from sys import stderr
from functools import reduce
from operator import add
def f(): return [int(i) for i in input().split()]
def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(repr(item))
        stderr.write(sep)
    stderr.write(end)



def eva(l, r, op):
    if op == "+":
        return l + r
    elif op == "-":
        return l - r
    elif op == "*":
        return l * r
    elif op == "/":
        return l // r


def calc(exp):
    debug(exp)
    i = 0
    num = deque()
    ops = deque()
    ope = ""
    while i < len(exp):
        if exp[i] == '(':
            end = 1
            s = 1
            while True:
                if exp[i+end] == "(":
                    s += 1
                elif exp[i+end] == ")":
                    s -= 1
                if s == 0:
                    break
                end += 1
            num.append(calc(exp[i+1:i+end]))
            i += end
            if ope:
                l = num.pop()
                r = num.pop()
                num.append(eva(l, r, ope))
                ope = ""
        elif exp[i] in ["*", "/"]:
            ope = exp[i]
        elif exp[i] in ["+", "-"]:
            ops.append(exp[i])
        elif exp[i] == "=":
            pass
        else:
            temp = ""
            j = 0
            while i + j < len(exp) and exp[i+j].isnumeric():
                temp += exp[i+j]
                j += 1
            num.append(int(temp))
            i += j - 1
            if ope:
                num.append(eva(num.pop(), num.pop(), ope))
                ope = ""
        i += 1
    while len(ops) != 0:
        l = num.popleft()
        r = num.popleft()
        num.appendleft(eva(l,r,ops.popleft()))
    
    return num[0]


n = int(input())
for i in range(n):
    exp = input()
    print(calc(exp))

Submission status: Runtime Error

Solution code:
for i in range(input()):
    print int((eval("".join([str(float(i)) if i.isdigit() else i for i in raw_i\
nput() if i!="="]))))

Submission status: Runtime Error

Solution code:
for i in range(input()):
    print int((eval("".join([str(float(i)) if i.isdigit() else i for i in raw_input() if i!="="]))))

Submission status: Runtime Error

Solution code:
for i in range(input()):
    s=raw_input()[:-1]
    s="".join(str(float(i)) if i.isdigit() else i for i in s)
    print int(eval(s))

Submission status: Runtime Error

Solution code:
def rank(ch,st):
    dic={"+":1,"-":1,"*":3,"/":2,"(":0}
    return dic[ch]<dic[st]
for i in range(input()):
    s=raw_input()[:-1]
    buf=[]
    stack=[]
    for ch in s:
        if ch.isdigit():
            buf.append(ch)
        elif ch==")":
            temp=stack.pop()
            while temp!="(":
                buf.append(temp)
                temp=stack.pop()
        elif ch=="(":
            stack.append(ch)
        elif stack==[]:
            stack.append(ch)
        elif rank(ch,stack[-1]):
            while stack!=[] and rank(ch,stack[-1]):
                buf.append(stack.pop())
            stack.append(ch)
        else:
            stack.append(ch)
    buf+=stack[::-1]
    stack=[]
    res=0
    for ch in buf:
        if ch.isdigit():
            stack.append(int(ch))
        else:
            a=stack.pop()
            b=stack.pop()
            if ch=="+":
                stack.append(a+b)
            elif ch=="-":
                stack.append(b-a)
            elif ch=="*":
                stack.append(a*b)
            else:
                stack.append(b/a)
    print stack[0]

Submission status: Runtime Error

Solution code:
def rank(ch,st):
    dic={"+":1,"-":1,"*":2,"/":2,"(":0,")":0}
    return dic[ch]<=dic[st]

for i in range(input()):
    s=raw_input()[:-1]
    buf=[]
    stack=[]
    for ch in s:
        if ch.isdigit():
            buf.append(ch)
        elif ch=="(":
            stack.append(ch)
        elif ch==")":
            temp=stack.pop()
            while temp!="(":
                buf.append(temp)
                temp=stack.pop()
        else:
            while stack!=[] and rank(ch,stack[-1]):
                buf.append(stack.pop())
            stack.append(ch)
    buf+=stack[::-1]

    stack=[]
    res=0
    for ch in buf:
        if ch.isdigit():
            stack.append(int(ch))
        else:
            a=stack.pop()
            b=stack.pop()
            if ch=="+":
                stack.append(a+b)
            elif ch=="-":
                stack.append(b-a)
            elif ch=="*":
                stack.append(a*b)
            else:
                stack.append(b/a)
    print stack[0]

Submission status: Runtime Error

Solution code:
from __future__ import division
for i in range(input()):
    s=raw_input()[:-1]
    t=""
    for ch in s:
        if ch.isdigit():
            t+=str(float(ch))
        elif ch=="/":
            t+="//"
        else:
            t+=ch
    print int(eval(t))

Submission status: Runtime Error

Solution code:
while True:
    n = int(raw_input())
    if n == 0:
        break

    for i in range(n):
        print eval(raw_input().strip()[:-1])

Submission status: Runtime Error

Solution code:
import sys

while True:
    line = raw_input()
    if line == '':
        break

    for i in range(int(line)):
        a = eval(raw_input().replace('/', '//')[:-1])
        sys.stderr.writelines(a)

Submission status: Runtime Error

Solution code:
import sys

while True:
    line = raw_input()
    if line == '':
        break

    for i in range(int(line)):
        a = eval(raw_input().replace('/', '//')[:-1])
        sys.stderr.write(a)

Submission status: Runtime Error

Solution code:
anb2 = lambda a,b: -(abs(int(1.0*a/b)))

def number(begin):
	global i
	
	if begin[i] == "(":
		i += 1
		res = expression(begin)
		i += 1 
		return res
	
	res = 0
	
	while begin[i].isdigit():
		res *= 10
		res += int(begin[i])
		i+=1
	
	return res
	
def term(begin):# your code goes here

	global i
	res = number(begin)
	
	while True:
		if begin[i] == "*":
			i += 1
			res *= number(begin)
		elif begin[i] == "/":
			i += 1
			num = number(begin)
			if res < 0 or num < 0:
				res = abs2(res,num)
			else:
				res /= num
		else:
			break
	return res
	
def expression(begin):
	
	global i
	res = term(begin)
	
	while True:
		if begin[i] == "+":
			i += 1
			res += term(begin)
			
		elif begin[i] == "-":
			i += 1
			res -= term(begin)
		else:
			break
	return res
	

def main():
	global i
	for j in range(input()):
			i = 0
			ex = raw_input() +"="
			ans = expression(ex)
	
			print ans
main()

Submission status: Runtime Error

Solution code:
def divide(x, y):
	s = 1
	if(x < 0):s *= -1
	if(y < 0):s *= -1
	return abs(x) / abs(y) * s
	

def _split(_src):
	c = 0
	l = len(_src)
	
	if l == 0:
		return []

	while c < l and ord('0') <= ord(_src[c]) <= ord('9'):
		c += 1
		
	if c == 0:
		return [_src[0]] + _split(_src[1:])
	
	else:
		return [int(_src[:c])] + _split(_src[c:])
		

def evaluate(src):
	if len(src) == 1:
		return src[0]
		
	if "(" in src:
		l = src.index("(")
		r = src.index(")")
		mid = evaluate(src[l + 1:r])
		return evaluate( src[:l] + [mid] + src[r + 1:])
	
	operators = [("+", lambda x,y:x + y),
				 ("-", lambda x,y:x - y),
				 ("*", lambda x,y:x * y),
				 ("/", divide)
				]	
	
	for (op,func) in operators:
		if op in src:
			p = src.index(op)
			l = evaluate(src[:p])
			r = evaluate(src[p + 1:])
			return func(l, r)
	
	
for t in xrange(input()):
	_src = raw_input()[:-1]
	src = _split(_src)
	print evaluate(src)

Submission status: Runtime Error

Solution code:
def divide(x, y):
	s = 1
	if(x < 0):s *= -1
	if(y < 0):s *= -1
	return abs(x) / abs(y) * s
	

def _split(_src):
	c = 0
	l = len(_src)
	if l == 0:
		return []
	while c < l and ord('0') <= ord(_src[c]) <= ord('9'):
		c += 1
	if c == 0:
		tmp = _split(_src[1:])
		if tmp != [] and tmp[0] == "-":
			tmp = ["+" , "(", 0 , "-", 1, ")", "*"] + tmp[1:]
		return [_src[0]] + tmp
	else:
		return [int(_src[:c])] + _split(_src[c:])
		

def evaluate(src):
	if len(src) == 1:
		return src[0]
		
	if "(" in src:
		l = src.index("(")
		r = src.index(")")
		mid = evaluate(src[l + 1:r])
		return evaluate( src[:l] + [mid] + src[r + 1:])
	
	operators = [("+", lambda x,y:x + y),
				 ("-", lambda x,y:x - y),
				 ("*", lambda x,y:x * y),
				 ("/", divide)
				]	
	
	for (op,func) in operators:
		if op in src:
			p = src.index(op)
			l = evaluate(src[:p])
			r = evaluate(src[p + 1:])
			return func(l, r)
	
	
for t in xrange(input()):
	_src = raw_input()[:-1]
	src = _split(_src)
	print evaluate(src)

Submission status: Runtime Error

Solution code:
def divide(x, y):
    s = 1
    if(x < 0):s *= -1
    if(y < 0):s *= -1
    if x == 0:exit(0)
    return abs(x) / abs(y) * s     
 
def _split(_src):
    c = 0
    l = len(_src)
    if l == 0:
        return []
    while c < l and ord('0') <= ord(_src[c]) <= ord('9'):
        c += 1
    if c == 0:
        tmp = _split(_src[1:])
        if tmp != [] and tmp[0] == "-":
            tmp = ["+" , "(", 0 , "-", 1, ")", "*"] + tmp[1:]
        return [_src[0]] + tmp
    else:
        return [int(_src[:c])] + _split(_src[c:])
         
 
def evaluate(src):
    if len(src) == 1:
        return src[0]
         
    if "(" in src:
        l = src.index("(")
        r = src.index(")")
        mid = evaluate(src[l + 1:r])
        return evaluate( src[:l] + [mid] + src[r + 1:])
     
    operators = [("+", lambda x,y:x + y),
                 ("-", lambda x,y:x - y),
                 ("*", lambda x,y:x * y),
                 ("/", divide)
                ]  
     
    for (op,func) in operators:
        if op in src:
            p = src.index(op)
            l = evaluate(src[:p])
            r = evaluate(src[p + 1:])
            return func(l, r)
     
     
for t in xrange(input()):
    _src = raw_input()[:-1]
    src = _split(_src)
    print evaluate(src)

Submission status: Runtime Error

Solution code:
def divide(x, y):
    s = 1
    if(x < 0):s *= -1
    if(y < 0):s *= -1
    if x == 0:exit(0)
    return abs(x) / abs(y) * s     
 
def _split(_src):
    c = 0
    l = len(_src)
    if l == 0:
        return []
    while c < l and ord('0') <= ord(_src[c]) <= ord('9'):
        c += 1
    if c == 0:
        tmp = _split(_src[1:])
        if tmp != [] and tmp[0] == "-":
            tmp = ["(", 0 , "-", 1, ")", "*"] + tmp[1:]
        return [_src[0]] + tmp
    else:
        return [int(_src[:c])] + _split(_src[c:])
         
 
def evaluate(src):
    if len(src) == 1:
        return src[0]
         
    if "(" in src:
        l = src.index("(")
        r = src.index(")")
        mid = evaluate(src[l + 1:r])
        return evaluate( src[:l] + [mid] + src[r + 1:])
     
    operators = [("+", lambda x,y:x + y),
                 ("-", lambda x,y:x - y),
                 ("*", lambda x,y:x * y),
                 ("/", divide)
                ]  
     
    for (op,func) in operators:
        if op in src:
            p = src.index(op)
            l = evaluate(src[:p])
            r = evaluate(src[p + 1:])
            return func(l, r)
     
     
for t in xrange(input()):
    _src = "0+" + raw_input()[:-1]
    src = _split(_src)
    print evaluate(src)

Submission status: Runtime Error

Solution code:
def divide(x, y):
    s = 1
    if(x < 0):s *= -1
    if(y < 0):s *= -1
    if x == 0:exit(0)
    return abs(x) / abs(y) * s     
 
def _split(_src):
    c = 0
    l = len(_src)
    if l == 0:
        return []
    while c < l and ord('0') <= ord(_src[c]) <= ord('9'):
        c += 1
    if c == 0:
        tmp = _split(_src[1:])
        if tmp != [] and tmp[0] == "-":
            tmp = ["(", 0 , "-", 1, ")", "*"] + tmp[1:]
        return [_src[0]] + tmp
    else:
        return [int(_src[:c])] + _split(_src[c:])
         
 
def evaluate(src):
    if len(src) == 1:
        return src[0]
         
    if "(" in src:
        l = src.index("(")
        r = src.index(")")
        mid = evaluate(src[l + 1:r])
        return evaluate( src[:l] + [mid] + src[r + 1:])
     
    operators = [("+", lambda x,y:x + y),
                 ("-", lambda x,y:x - y),
                 ("*", lambda x,y:x * y),
                 ("/", divide)
                ]  
     
    for (op,func) in operators:
        if op in src:
            p = src.index(op)
            l = evaluate(src[:p])
            r = evaluate(src[p + 1:])
            return func(l, r)
    exit()
     
for t in xrange(input()):
    _src = "0+" + raw_input()[:-1]
    src = _split(_src)
    print evaluate(src)

Submission status: Runtime Error

Solution code:
def divide(x, y):
    s = 1
    if(x < 0):s *= -1
    if(y < 0):s *= -1
    if x == 0:exit()
    return abs(x) / abs(y) * s     
 
def _split(_src):
    c = 0
    l = len(_src)
    if l == 0:
        return []
    while c < l and ord('0') <= ord(_src[c]) <= ord('9'):
        c += 1
    if c == 0:
        tmp = _split(_src[1:])
        if tmp != [] and tmp[0] == "-":
            tmp = ["(", 0 , "-", 1, ")", "*"] + tmp[1:]
        return [_src[0]] + tmp
    else:
        return [int(_src[:c])] + _split(_src[c:])
         
 
def evaluate(src):
    if len(src) == 1:
        return src[0]
         
    if "(" in src:
        l = src.index("(")
        r = src.index(")")
        mid = evaluate(src[l + 1:r])
        return evaluate( src[:l] + [mid] + src[r + 1:])
     
    operators = [("+", lambda x,y:x + y),
                 ("-", lambda x,y:x - y),
                 ("*", lambda x,y:x * y),
                 ("/", divide)
                ]  
     
    for (op,func) in operators:
        if op in src:
            p = src.index(op)
            l = evaluate(src[:p])
            r = evaluate(src[p + 1:])
            return func(l, r)
     
for t in xrange(input()):
    _src = "0+" + raw_input().strip("=")
    src = _split(_src)
    print evaluate(src)

Submission status: Runtime Error

Solution code:
def divide(x, y):
    s = 1
    if(x < 0):s *= -1
    if(y < 0):s *= -1
    if x == 0:exit()
    return abs(x) / abs(y) * s     
 
def _split(_src):
    c = 0
    l = len(_src)
    if l == 0:
        return []
    while c < l and ord('0') <= ord(_src[c]) <= ord('9'):
        c += 1
    if c == 0:
        return [_src[0]] + _split(_src[1:])
    else:
        return [int(_src[:c])] + _split(_src[c:])
         
 
def evaluate(src):
    if len(src) == 1:
        return src[0]
         
    if "(" in src:
        l = src.index("(")
        r = len(src) - 1 - src[::-1].index(")")
        mid = evaluate(src[l + 1:r])
        return evaluate( src[:l] + [mid] + src[r + 1:])
     
    operators = [("+", lambda x,y:x + y),
                 ("-", lambda x,y:x - y),
                 ("*", lambda x,y:x * y),
                 ("/", divide)
                ]  
     
    for (op,func) in operators:
        if op in src:
            p = src.index(op)
            l = evaluate(src[:p])
            r = evaluate(src[p + 1:])
            return func(l, r)
     
for t in xrange(input()):
    _src = raw_input()[:-1]
    src = _split(_src)
    print evaluate(src)

Submission status: Runtime Error

Solution code:
def divide(x, y):
    s = 1
    if(x < 0):s *= -1
    if(y < 0):s *= -1
    if x == 0:exit()
    return abs(x) / abs(y) * s     
 
def _split(_src):
    c = 0
    l = len(_src)
    if l == 0:
        return []
    while c < l and ord('0') <= ord(_src[c]) <= ord('9'):
        c += 1
    if c == 0:
        tmp = _split(_src[1:])
        if tmp != [] and tmp[0] == "-":
            tmp = ["(", 0 , "-", 1, ")", "*"] + tmp[1:]
        return [_src[0]] + tmp
    else:
        return [int(_src[:c])] + _split(_src[c:])
         
 
def evaluate(src):
    if len(src) == 1:
        return src[0]
         
    if "(" in src:
        l = src.index("(")
        r = len(src) - 1 - src[::-1].index(")")
        mid = evaluate(src[l + 1:r])
        return evaluate( src[:l] + [mid] + src[r + 1:])
     
    operators = [("+", lambda x,y:x + y),
                 ("-", lambda x,y:x - y),
                 ("*", lambda x,y:x * y),
                 ("/", divide)
                ]  
     
    for (op,func) in operators:
        if op in src:
            p = src.index(op)
            l = evaluate(src[:p])
            r = evaluate(src[p + 1:])
            return func(l, r)
     
for t in xrange(input()):
    _src = "0+" + raw_input().strip("=")
    src = _split(_src)
    print evaluate(src)

Submission status: Runtime Error

Solution code:
def divide(x, y):
    s = 1
    if(x < 0):s *= -1
    if(y < 0):s *= -1
    return abs(x) / abs(y) * s     
 
def _split(_src):
    c = 0
    l = len(_src)
    if l == 0:
        return []
    while c < l and ord('0') <= ord(_src[c]) <= ord('9'):
        c += 1
    if c == 0:
        tmp = _split(_src[1:])
        if tmp != [] and tmp[0] == "-":
            tmp = ["(", 0 , "-", 1, ")", "*"] + tmp[1:]
        return [_src[0]] + tmp
    else:
        return [int(_src[:c])] + _split(_src[c:])
         
 
def evaluate(src):
    if len(src) == 1:
        return src[0]
         
    if "(" in src:
        l = src.index("(")
        r = l
        st = 1
        while st:
            r += 1
            if src[r] == "(": st += 1
            if src[r] == ")": st -= 1
        
        
        mid = evaluate(src[l + 1:r])
        return evaluate( src[:l] + [mid] + src[r + 1:])
     
    operators = [("+", lambda x,y:x + y),
                 ("-", lambda x,y:x - y),
                 ("*", lambda x,y:x * y),
                 ("/", divide)
                ]  
     
    for (op,func) in operators:
        if op in src:
            p = src.index(op)
            l = evaluate(src[:p])
            r = evaluate(src[p + 1:])
            return func(l, r)
     
for t in xrange(input()):
    _src = "0+" + raw_input().strip("=")
    src = _split(_src)
    print evaluate(src)

Submission status: Runtime Error

Solution code:
for i in xrange(input()):
	print int(eval(raw_input().strip().strip('=').replace('/','.0/')))

Submission status: Runtime Error

Solution code:
T = int(input())
for tc in range(0,T) :
	print input().strip("=")

Submission status: Runtime Error

Solution code:
import sys
for exp in sys.stdin:
    if exp[-1] == '=':
        print eval(exp[1:])
    else:
        print eval(exp)