Generation

generate functionMon, 16 Jan 2023

create math expression parser

def parse(expr): stack = [] for char in expr: if char not in ['+', '-', '*', '/']: stack.append(int(char)) elif char == '-': left = stack.pop() right = stack.pop() stack.append(right - left) elif char == '+': left = stack.pop() right = stack.pop() stack.append(right + left) elif char == '*': left = stack.pop() right = stack.pop() stack.append(right * left) elif char == '/': left = stack.pop() right = stack.pop() stack.append(right / left) return stack.pop() parse("231*+9-")

Questions about programming?Chat with your personal AI assistant