Skip to content

Instantly share code, notes, and snippets.

@retrage
Created June 11, 2014 04:01
Show Gist options
  • Save retrage/9184210ad61980299c47 to your computer and use it in GitHub Desktop.
Save retrage/9184210ad61980299c47 to your computer and use it in GitHub Desktop.
calc
def parse3(l):
stack = []
for i in range(len(l)):
if l[i]=='(':
stack.insert(0, i)
elif l[i]==')':
print l[stack.pop(0):i+1]
def calc(l):
ll = len(l)
stack = []
i = 0
while(i<ll):
if l[i]=='(':
stack.insert(0, i)
elif l[i]==')':
b = stack.pop(0)
e = l[b+1:i].split(' ')
p = e.pop(0)
n = int(e.pop(0))
for j in e:
n = eval('n'+p+j)
l = l[:b] + str(n) + l[i+1:]
i = b
i+=1
ll = len(l)
return int(l)
if __name__=='__main__':
while 1:
s = raw_input('-->')
print calc(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment