def infix_to_postfix(infix_expression): """ Converts an infix expression to a postfix expression. Numerical operands must be integers. >>> infix_to_postfix('2 + 3') '2 3 +' >>> infix_to_postfix('2 + 3 * 4') '2 3 4 * +' >>> infix_to_postfix('(2 + 3) * 4') '2 3 + 4 *' >>> infix_to_postfix('2 + 3 * 2 - 5') '2 3 2 * + 5 -' >>> infix_to_postfix('(2 + 3) * (2 - 5)') '2 3 + 2 5 - *' >>> infix_to_postfix('(2 + 3) * (5 / 2)') '2 3 + 5 2 / *' """ # Split infix string into tokens. For example: # '2+3*4' => ['2', '+', '3', '*', '4'] tokens = re.findall(r'(\d+|\*|\+|\/|\-|\)|\(|\^)', infix_expression) output = [] stack = Stack() for s in tokens: if s not in OP_PREC: output.append(s) elif s == '(': stack.push(s) elif s == ')': item = stack.pop() while item != '(': output.append(item) item = stack.pop() else: while not stack.is_empty() and OP_PREC[s] <= OP_PREC[stack.peek()]: output.append(stack.peek()) stack.pop() stack.push(s) while not stack.is_empty(): output.append(stack.pop()) string = '' count = 0 for i in range(len(output) - 1): string += output[i] string += ' ' count += 1 string += output[-1] return string
def test_stack(): t = Stack([54, 27]) print(t.peek()) t.clear() # t.peek() t.push(10) t.push(15) t.push(20) print(t) t.clear() print(t) t.push(2) print(t) print(t.peek())
def evaluate_infix(infix_expression): """ Evaluates an infix expression. Numerical operands must be integers. >>> evaluate_infix('2 + 3 * 4') 14 >>> evaluate_infix('2 + (3 * 4)') 14 >>> evaluate_infix('(2 + 3) * 4') 20 >>> evaluate_infix('2 + 3 * 2 - 5') 3 >>> evaluate_infix('(2 + 3) * (2 - 5)') -15 >>> evaluate_infix('(2 + 3) * (5 / 2)') 12.5 """ # Split infix string into tokens. For example: # '2+3*4' => ['2', '+', '3', '*', '4'] tokens = re.findall(r'(\d+|\*|\+|\/|\-|\)|\(|\^)', infix_expression) operator = Stack() operand = Stack() for s in tokens: if s not in OP_PREC: #s is an integer operand.push(s) elif s == '(': operator.push(s) elif s == ')': item = operator.pop() while item != '(': alpha = float(operand.pop()) beta = float(operand.pop()) if item == '(': operator.pop() else: delta = calculate(item, beta, alpha) operand.push(delta) item = operator.pop() else: #s is an operator if len(operator) > 1 and len(operand) > 2: if OP_PREC[s] < OP_PREC[operator.peek()]: sign = operator.pop() alpha = float(operand.pop()) beta = float(operand.pop()) delta = calculate(sign, beta, alpha) operand.push(delta) operator.push(s) while not operand.is_empty() and not operator.is_empty(): sign = operator.pop() alpha = float(operand.pop()) beta = float(operand.pop()) delta = calculate(sign, beta, alpha) operand.push(delta) result = operand.pop() if result % int(result) == 0: result = round(result) return result
def check_relation(label, x): # todo : need to add negation to each label. # label : a list of time_signature # x : a query for its label stack = Stack() unpop = [] modified = True for node in label: if node < x: if node.type == 'start': stack.push(node) # print('push') if node.type == 'end': if node.relation == stack.peek().relation: stack.pop() # print('pop') while (unpop and unpop[-1] == stack.peek().relation): stack.pop() # print('pop') unpop = unpop[:-1] else: unpop.append(node.relation) else: if not modified: if node.type == 'end' and stack.peek( ).relation[:3] == 'NOT' and node.relation[:3] != 'NOT': stack.push( time_signature('0000-00-00', relation=node.relation)) if stack.size() == 1: rel = node.relation else: rel = stack.peek().relation return rel else: rel = stack.peek().relation return rel
# print(lst) # print(lst.nodes) # stack = Stack() # stack.push(4) # stack.push(5) # stack.push(6) # print(stack.pop()) # stack.push(7) # print(stack) # print(stack.count) print('Stack:') stack = Stack([1, 2, 3]) print(stack) print(stack.peek()) stack.pop() print(stack) print(stack.peek()) print('Queue:') queue = Queue([1, 2, 3]) print(queue) print(queue.peek()) queue.dequeue() print(queue) print(queue.peek())