def parse_expression(tokens): s = Stack() p = {')': '(', ']': '[', '}': '{'} for i in tokens: try: i = BinaryTree(int(i)) except ValueError: pass if i not in p: s.push(i) else: try: arg2 = s.pop() operator = s.pop() arg1 = s.pop() symbol = s.pop() if symbol != p[i]: return tree = BinaryTree(operator) tree.left_node = arg1 tree.right_node = arg2 s.push(tree) except EmptyStackError: return if s.is_empty(): return tree = s.pop() if s.is_empty(): return tree else: return
def evaluate_expression(tokens): paranthesis = {')':'(','}':'{','[':']'} stack = Stack() for token in tokens: try: token = int(token) except ValueError: pass if token not in paranthesis: stack.push(token) else: try : arg_2 = stack.pop() arg_1 = stack.pop() operator = stack.pop() opening_paranthesis = stack.pop() if opening_paranthesis != paranthesis[token]: return stack.push({'+': add, '-': sub, '*': mul, '/':truediv}[operator](arg_1, arg_2)) except EmptyStackError: return if stack.is_empty(): return value = stack.pop() if not stack.is_empty(): return return value
def evaluate(expression): if any(not (c.isdigit() or c.isspace() or c in '()[]{}+-*/') for c in expression): return # Tokens can be natural numbers, (, ), [, ], {, }, +, -, *, and / tokens = re.compile('(\d+|\(|\)|\[|\]|{|}|\+|-|\*|/)').findall(expression) #try: parentheses = {')': '(', ']': '[', '}': '{'} stack = Stack() for token in tokens: try: token = int(token) except ValueError: pass if token not in parentheses: stack.push(token) else: try: arg_2 = stack.pop() operator = stack.pop() arg_1 = stack.pop() opening_group_symbol = stack.pop() if parentheses[token] != opening_group_symbol: return stack.push({'+': add, '-': sub, '*': mul, '/': truediv}[operator](arg_1, arg_2)) except EmptyStackError: return if stack.is_empty(): return value = stack.pop() if not stack.is_empty(): return return value
def parse_expression(tokens): paranthesis = {')': '(', '}': '{', ']': '['} stack = Stack() for token in tokens: try: token = BinaryTree(int(token)) except ValueError: pass if token not in paranthesis: stack.push(token) else: try: arg_2 = stack.pop() operator = stack.pop() arg_1 = stack.pop() opening_paranthesis = stack.pop() if opening_paranthesis != paranthesis[token]: return #stack.push({'+': add, '-': sub, '*': mul, '/':truediv}[operator](arg_1, arg_2)) parse_tree = BinaryTree(operator) parse_tree.right_node = arg_2 parse_tree.left_node = arg_1 stack.push(parse_tree) except EmptyStackError: return if stack.is_empty(): return value = stack.pop() if not stack.is_empty(): return return parse_tree
def evaluate_expression(tokens): s = Stack() p = {')': '(', '}': '{', ']': '['} for i in tokens: try: i = int(i) except ValueError: pass if i not in p: s.push(i) else: try: arg2 = s.pop() operate = s.pop() arg1 = s.pop() symbol = s.pop() if symbol != p[i]: return s.push({ '+': add, '-': sub, '*': mul, '/': truediv }[operate](arg1, arg2)) except EmptyStackError: return if s.is_empty(): return value = s.pop() if s.is_empty(): return value else: return
def evaluate_expression(tokens): par_dict = {')':'(','}':'{',']':'['} stack = Stack() # print(tokens) for token in tokens: # print(token) try: token = int(token) # print(token) stack.push(token) except ValueError: if token in {']',')','}'}: try: arg_2 = stack.pop() method = stack.pop() arg_1 = stack.pop() par = stack.pop() if par != par_dict[token]: return if method not in {add,sub,mul,truediv}: return stack.push(method(arg_1,arg_2)) except EmptyStackError: return elif token in par_dict.values(): stack.push(token) else: stack.push({'+':add,'-':sub,'*':mul,'/':truediv}[token]) # print(stack.peek()) if stack.is_empty(): return value = stack.pop() if not stack.is_empty(): return return value
def evaluate_expression(tokens): stack = Stack() for token in tokens: try: token = int(token) stack.push(token) except ValueError: try: arg_2 = stack.pop() arg_1 = stack.pop() stack.push({ '+': add, '-': sub, '*': mul, '/': truediv }[token](arg_1, arg_2)) except EmptyStackError: return if stack.is_empty(): return value = stack.pop() print(value) if not stack.is_empty(): return return value
def evaluate_postfix_expression(expression): stack = Stack() first_digit = False for e in expression: if e.isdigit(): if first_digit == False: stack.push(int(e)) first_digit = True else: stack.push(stack.pop() * 10 + int(e)) else: first_digit = False if e in '+-*/': arg_2 = stack.pop() arg_1 = stack.pop() stack.push({ '+': add, '-': sub, '*': mul, '/': truediv }[e](arg_1, arg_2)) if stack.is_empty(): return value = stack.pop() if not stack.is_empty(): return return value
def parse_expression(tokens):#equal to evaluate_expression(tokens)#below are my code parenthese = {')':'(','}':'{',']':'['} stack = Stack() for token in tokens: try: token = BinaryTree(int(token)) except ValueError: pass if token not in parenthese: stack.push(token) else: try: arg_2 = stack.pop() operater = stack.pop() arg_1 = stack.pop() former = stack.pop() if parenthese[token] == former: my_tree = BinaryTree(operater) my_tree.left_node = arg_1 my_tree.right_node = arg_2 stack.push(my_tree) else: return except EmptyStackError: return if stack.is_empty(): return my_tree = stack.pop() if not stack.is_empty(): return return my_tree
def parse_expression(tokens): parentheses = {')': '(', ']': '[', '}': '{'} stack = Stack() for token in tokens: try: token = BinaryTree(int(token)) #*************************** except ValueError: pass if token not in parentheses: stack.push(token) else: try: arg_2 = stack.pop() operator = stack.pop() arg_1 = stack.pop() opening_group_symbol = stack.pop() if parentheses[token] != opening_group_symbol: return #****************************************************** parse_tree = BinaryTree(operator) parse_tree.left_node = arg_1 parse_tree.right_node = arg_2 stack.push(parse_tree) #***************************************************** except EmptyStackError: return if stack.is_empty(): return parse_tree = stack.pop() if not stack.is_empty(): return return parse_tree
def parse_expression(tokens): parentheses = {')': '(', ']': '[', '}': '{'} stack = Stack() # tree = BinaryTree() for token in tokens: try: token = BinaryTree(int(token)) except ValueError: pass if token not in parentheses: stack.push(token) else: try: arg_2 = stack.pop() operator = stack.pop() arg_1 = stack.pop() symbol = stack.pop() if symbol != parentheses[token]: return tree = BinaryTree(operator) tree.left_node = arg_1 tree.right_node = arg_2 stack.push(tree) # stack.push({'+': add, '-': sub, '*': mul, '/': truediv}[operator](arg_1, arg_2)) except EmptyStackError: return if stack.is_empty(): return tree = stack.pop() if not stack.is_empty(): return return tree
def evaluate_expression(tokens): stack = Stack() parenthese = {')': '(', ']': '[', '}': '{'} for token in tokens: try: token = int(token) except ValueError: pass if token not in parenthese: stack.push(token) else: try: arg_2 = stack.pop() operator = stack.pop() arg_1 = stack.pop() former = stack.pop() if parenthese[token] == former: stack.push({ '+': add, '-': sub, '*': mul, '/': truediv }[operator](arg_1, arg_2)) else: return except EmptyStackError: return if stack.is_empty(): return value = stack.pop() if not stack.is_empty(): return return value
def evaluate(expression): for c in expression: if not c.isdigit() and not c.isspace() and c not in '(){}[]+-*/': return tokens = re.compile('(\d+|\(|\)|{|}|\[|\]|\+|-|\*|/)').findall(expression) try: paranthesis = {')': '(', ']': '[', '}': '{'} stack = Stack() for token in tokens: try: token = int(token) except ValueError: pass if token not in paranthesis: stack.push(token) else: try: arg1 = stack.pop() operator = stack.pop() arg2 = stack.pop() opening_paranthesis = stack.pop() if opening_paranthesis != paranthesis[token]: return stack.push({ '+': add, '-': sub, '*': mul, '/': truediv }[operator](arg1, arg2)) except EmptyStackError: return if stack.is_empty(): return value = stack.pop() if not stack.is_empty(): return return value except ZeroDivisionError: return
def parse_expression(tokens): # Replace pass above with your code, modified from the exercise: # Evaluate fully parenthetised expressions with a stack. stack = Stack() oppo_brace = {')': '(', ']': '[', '}': '{'} for token in tokens: if not (token == ')' or token == ']' or token == '}'): if not (token == '(' or token == '[' or token == '{' \ or token == '+' or token == '-' or token == '*' \ or token == '/'): token = BinaryTree(int(token)) stack.push(token) else: stack.push(token) else: oppo = oppo_brace[token] try: arg_3 = stack.pop() arg_2 = stack.pop() arg_1 = stack.pop() arg = stack.pop() if arg != oppo: return except EmptyStackError: return #stack.push({'+': add, '-': sub, '*': mul, '/': truediv}[arg_2](arg_1, arg_3)) parse_tree = BinaryTree(arg_2) parse_tree.left_node = arg_1 parse_tree.right_node = arg_3 stack.push(parse_tree) if stack.is_empty(): return parse_tree = stack.pop() if not stack.is_empty(): return return parse_tree
def evaluate_expression(tokens): parentheses = {')': '(', ']': '[', '}': '{'} stack = Stack() print(stack) for token in tokens: try: token = int(token) except ValueError: pass if token not in parentheses: stack.push(token) print(stack) else: try: arg_2 = stack.pop() operator = stack.pop() arg_1 = stack.pop() opening_group_symbol = stack.pop() if parentheses[token] != opening_group_symbol: return stack.push({ '+': add, '-': sub, '*': mul, '/': truediv }[operator](arg_1, arg_2)) except EmptyStackError: return if stack.is_empty(): return value = stack.pop() if not stack.is_empty(): return return value
def calcualte_infix_string(string): # include the parentheses and others and spaces needed separator_library = { ')': '(', ']': '[', '}': '{' } operator_library = { '+': lambda x, y: x + y, '-': lambda x, y: y - x, '*': lambda x, y: x * y, '/': lambda x, y: y / x, '//': lambda x, y: y // x, '%': lambda x, y: y % x } string = string.strip() string = re.sub(' +', ' ', string) op_list = string.split(' ') # cp_list=op_list[:] # print(op_list) stack = Stack() # flag=False while op_list: # flag=True current_char = op_list.pop(0) # print(current_char) if current_char in separator_library.keys(): target_char = separator_library[current_char] temp_list = [] operator = None count=0 while stack.peek() != target_char: temp = stack.pop() if temp in operator_library.keys(): operator = temp count+=1 else: temp_list.append(temp) if count>1 or count==0: return None if len(temp_list)>2: return None stack.pop() if operator == '-' and len(temp_list) == 1: stack.push(temp_list[0]*-1) elif (operator == '/' or operator=='//') and temp_list[0]==0: return None else: stack.push(operator_library[operator](temp_list[0], temp_list[-1])) # print(f'{operator},{temp_list}') else: try: stack.push(int(current_char)) except: stack.push(current_char) # reflag=False # if flag: # for each in string: # if each in separator_library.keys(): # reflag=True # break # if not reflag: # if len(cp_list)>1: # return None if len(stack) > 1: temp_list = [] operator = None count=0 while not stack.is_empty(): temp = stack.pop() if temp in operator_library.keys(): operator = temp count+=1 else: temp_list.append(temp) if count>1 or count==0: return None if len(temp_list)>2: return None if operator is not None: return None else: return temp_list[0] # if operator == '-' and len(temp_list) == 1: # return temp_list[0] * -1 # elif (operator == '/' or operator == '//') and temp_list[0] == 0: # return None # else: # return operator_library[operator](temp_list[0], temp_list[-1]) elif stack.is_empty(): return 0 else: return stack.pop()