def check_bal_par(par_str): if not len(par_str): return True if len(par_str) % 2 != 0: return False s = Stack() for item in par_str: if s.is_empty(): s.push(item) else: if s.peek() == '(' and item == ')': s.pop() else: s.push(item) return s.is_empty()
def is_valid(self, s): array_stack = Stack() str_list = list(s) for index, char in enumerate(str_list): if char == '(' or char == '{' or char == '[': array_stack.push(char) else: if array_stack.is_empty(): return False top_char = array_stack.pop() if char == ')' and top_char != '(': return False if char == ']' and top_char != '[': return False if char == '}' and top_char != '{': return False return array_stack.is_empty()
from stack.Stack import Stack s = Stack() print(s.get_size()) print(s.is_empty()) print(s.peek()) s.print_stack() s.push(5) s.push(2) s.push(28) s.push(11) s.push(18) print(s.get_size()) print(s.is_empty()) print(s.peek()) s.print_stack() print(s.pop()) print(s.pop()) print(s.pop()) print(s.pop()) print(s.pop()) s.print_stack()