def check_str(str_main): str_unic = '/' str_left = '{[' str_right = '}]' for ch in str_main: if ch not in str_left and ch not in str_right and ch not in str_unic: continue if ch in str_unic: ch_back = my_stack.pop() if ch_back in str_unic: if ch_back != ch: return False else: my_stack.push(ch_back) my_stack.push(ch) elif ch in str_right: ch_back = my_stack.pop() if (ch == '}' and ch_back != '{' or ch == ']' and ch_back != '['): return False else: my_stack.push(ch) if my_stack.is_empty(): return True return False
def polska9_2(mas_token): mas_end = [] for ch_next in mas_token[::-1]: if ch_next == ')': my_stack.push((ch_next, 0)) elif ch_next == '(': # переделать обработку ch_old = my_stack.pop() while ch_old[0] != ')': mas_end.append(ch_old[0]) ch_old = my_stack.pop() elif ch_next.isdigit(): mas_end.append(ch_next) else: # Иначе это функция - действие prior = get_prioryty(ch_next) if my_stack.is_empty(): my_stack.push((ch_next, prior)) else: ch_old = my_stack.pop() if ch_old[1] >= prior: mas_end.append(ch_old[0]) else: my_stack.push(ch_old) my_stack.push((ch_next, prior)) while not my_stack.is_empty(): ch_old = my_stack.pop() mas_end.append(ch_old[0]) return mas_end[::-1]
def calculator(mas_token): my_stack.clear() for ch_next in mas_token: if ch_next.isdigit(): my_stack.push(int(ch_next)) else: if my_stack.is_empty(): print('Выражение записано не верно. Ошибка.') exit(0) d2 = my_stack.pop() if my_stack.is_empty(): print('Выражение записано не верно. Ошибка.') exit(0) d1 = my_stack.pop() if ch_next == '-': my_stack.push(d1-d2) elif ch_next == '+': my_stack.push(d1+d2) elif ch_next == '*': my_stack.push(d1*d2) elif ch_next == '/': if d2 == 0: print('Деление на 0. Ошибка.') exit(0) my_stack.push(d1/d2) elif ch_next == '^': my_stack.push(d1**d2) else: print('ERROR!!!') exit(3) return my_stack.pop()
def runDepthSearch(G): my_stack.clear() my_stack.push('a') visited = ['a'] while len(my_stack.stack) > 0: node = my_stack.pop() # print('pop',node, my_stack.stack) tracker.append(node) nd = list(G.neighbors(node)) # G[node] for x in nd: if x not in visited: visited.append(x) my_stack.push(x) # print('push',x, my_stack.stack) return tracker
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import my_stack if __name__ == "__main__": my_stack.clear() for i in range(1, 11): my_stack.push(i) while not (my_stack.is_empty()): x = my_stack.pop() print(x)