def infix_to_postfix(infix_expr): precedence = {} precedence["*"] = 3 precedence["/"] = 3 precedence["+"] = 2 precedence["-"] = 2 precedence["("] = 1 op_stack = Stack() postfix_list = [] token_list = infix_expr.split() for token in token_list: if token in "ABCDEFGHIJKLLMNOPQRSTUVWXYZ" or token in "0123456789": postfix_list.append(token) elif token == '(': op_stack.push(token) elif token == ')': top_token = op_stack.pop() while top_token != '(': postfix_list.append(top_token) top_token = op_stack.pop() else: while (not op_stack.is_empty()) and (precedence[op_stack.peek()] >= precedence[token]): postfix_list.append(op_stack.pop()) op_stack.push(token) while not op_stack.is_empty(): postfix_list.append(op_stack.pop()) return "".join(postfix_list)
def postfix_eval(postfix_expr): operand_stack = Stack() token_list = postfix_expr.split() for token in token_list: if token in "0123456789": operand_stack.push(token) else: operand2 = operand_stack.pop() operand1 = operand_stack.pop() result = do_math(token, int(operand1), int(operand2)) operand_stack.push(result) return operand_stack.pop()
def par_checker(symbol_string): s = Stack() balanced = True index = 0 while index < len(symbol_string) and balanced: symbol = symbol_string[index] if symbol == '(': s.push(symbol) else: if s.is_empty(): balanced = False else: s.pop() index += 1 return balanced and s.is_empty()
def divide_by_2(dec_number): rem_stack = Stack() while dec_number > 0: rem = dec_number % 2 rem_stack.push(rem) dec_number //= 2 # // integer division bin_string = "" while not rem_stack.is_empty(): bin_string += str(rem_stack.pop()) return bin_string
def base_converter(dec_number, base): rem_stack = Stack() while dec_number > 0: rem = dec_number % base rem_stack.push(rem) dec_number //= base # // integer division base_string = "" while not rem_stack.is_empty(): base_string += str(rem_stack.pop()) return base_string
def build_parse_tree(mathexp): tokens = mathexp.split() p_stack = Stack() e_tree = BinaryTree('') p_stack.push(e_tree) curr = e_tree for t in tokens: if t == '(': curr.insert_left('') p_stack.push(curr) curr = curr.get_left_child() elif t not in ['+', '-', '*', '/', ')']: curr.set_root_val(int(t)) curr = p_stack.pop() elif t in ['+', '-', '*', '/']: curr.set_root_val(t) curr.insert_right('') p_stack.push(curr) curr = curr.get_right_child() elif t == ')': curr = p_stack.pop() else: raise ValueError return e_tree
def par_checker(symbol_string): s = Stack() balanced = True index = 0 while index < len(symbol_string) and balanced: symbol = symbol_string[index] if symbol in '([{': s.push(symbol) else: if s.is_empty(): balanced = False else: top = s.pop() if not matches(top, symbol): balanced = False index += 1 return balanced and s.is_empty()
def aStarSearch(prob): # A* search pq = PriorityQueue() # hang doi uu tien occ = {} # dung de kiem tra xem mot state co duoc expand chua dist = {} # so luong step can de di tu start den cac node traverse = {} # dung de truy vet startState = prob.getStartState() # trang thai ban dau start = startState.getPacmanPosition( ) # vi tri ban dau cua pacman, cung la vi tri cua S dist[start] = 0 # so luong step can de di tu start den goal la 0 pq.push(start, prob.getScore(start)) top = 0 # khai bao bien lay top cua priority queue while pq.isEmpty() is False: # lap cho den khi goal node duoc expand top = pq.pop() if prob.isGoalState( top): # kiem tra xem co phai la trang thai ket thuc break if occ.get(top, 0) != 0: # kiem tra mot trang thai co duoc expand chua continue occ[top] = 1 successors = prob.getChilds(top) for s in successors: # Neu TH1) trang thai hien tai chua tung duoc explore , # hay TH2) duoc explore roi nhung chua duoc expand va so step moi # nho hon so step cu de di duoc den trang thai nay if (dist.get(s, -1) == -1) or (occ.get(s, 0) == 0 and dist[s] > dist[top] + 1): dist[s] = dist[top] + 1 traverse[s] = top pq.push(s, dist[s] + prob.getScore(s)) if prob.isGoalState(top) is False: return -1 q = Stack( ) # Tu day cho den het function nay la de lay list cac step de di tu S den G node = top while traverse.get(node, -1) != -1: q.push(node) node = traverse[node] q.push(node) path = [] while q.isEmpty() is False: top = q.pop() path.append(top) return path