Skip to content

Instantly share code, notes, and snippets.

@sooop

sooop/calc.py Secret

Last active July 30, 2018 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sooop/50e7de7d5ec308e5eebc to your computer and use it in GitHub Desktop.
Save sooop/50e7de7d5ec308e5eebc to your computer and use it in GitHub Desktop.
import re
import sys
def tokenize(expStr):
pat = re.compile(r'(?:(?<=[^\d\.])(?=\d)|(?=[^\d\.]))', re.MULTILINE)
return [x for x in re.sub(pat, ' ', expStr).split(' ') if x]
def parse_expr(expStr):
tokens = tokenize(expStr)
op = dict(zip('*/+-()', (50, 50, 40, 40, 0, 0)))
output = []
stack = []
for item in tokens:
if item not in op:
output.append(item)
elif item == '(':
stack.append(item)
elif item == ')':
while stack != [] and \
stack[-1] != '(':
output.append(stack.pop())
stack.pop()
else:
while stack != [] and \
op[stack[-1]] >= op[item]:
output.append(stack.pop())
stack.append(item)
while stack:
output.append(stack.pop())
print(output)
return output
def calc_expr(tokens):
operations = {
'*': lambda x, y: y * x,
'/': lambda x, y: y / x,
'+': lambda x, y: y + x,
'-': lambda x, y: y - x
}
stack = []
for item in tokens:
if item not in operations:
if '.' in item:
stack.append(float(item))
else:
stack.append(int(item))
else:
x = stack.pop()
y = stack.pop()
stack.append(operations[item](x, y))
return stack[-1]
def process(expStr):
print(calc_expr(parse_expr(expStr)))
def main():
if len(sys.argv) > 1:
x = ' '.join(sys.argv[1:])
else:
x = input()
process(x)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment