def main(): s = Stack() print(len(s)) s.push(1) print(s.top()) print(s.is_empty()) print(s.pop()) print(s.is_empty()) try: print(s.pop()) except IndexError: print('Error') try: print(s.top()) except IndexError: print('Error') s.push(1) s.push(2) s.push(3) print(len(s)) print(s.pop()) print(s.pop()) print(s.pop()) print(s.is_empty())
def matcher(line): left = "({[" right = "]})" square = "[]" curl = "{}" roun = "()" s = Stack() for char in line: try: if char in left: s.push(char) elif char in right and s.top() in square and char in square: s.pop() elif char in right and s.top() in curl and char in curl: s.pop() elif char in right and s.top() in roun and char in roun: s.pop() except IndexError: return False return s.is_empty()
def matcher(line): d = {')': '(', '}': '{', ']': '['} s = Stack() for c in line: if c in d.values(): s.push(c) elif c in d.keys(): if s.is_empty(): return False if s.top() != d[c]: return False s.pop() return s.is_empty()
def matcher(s): stack = Stack() for e in s: if e in lefties: stack.push(e) try: if e in righties and stack.top() == mapping[e]: stack.pop() except IndexError: return False if stack.is_empty(): return True return False
def matcher(l): s = Stack() for c in l: if c in lefties: s.push(c) try: if c in righties and s.top() == dictr[c]: s.pop() except IndexError: return False if s.is_empty(): return True return False
def matcher(l): if len(l) % 2 == 1: return False l = list(l) l.sort() l = "".join(l) lefts = "{[(" rights = "}])" s = Stack() i = 0 while i < len(l): if l[i] in lefts: s.push(l[i]) elif l[i] in rights and len(s.l) > 0: s.pop() i += 1 return s.is_empty()
def matcher(line): s = Stack() left = '({[' right = ')}]' brackets = ['()', '{}', '[]'] line = line.strip() try: for ch in line: if ch in left: s.push(ch) else: if s.top() + ch in brackets: s.pop() return s.is_empty() except: return False
def matcher(line): s = Stack() lefties = '({[' righties = ')}]' brackets = ['()', '{}', '[]'] line = line.strip() try: for pattern in line: if pattern in lefties: s.push(pattern) else: if s.top() + pattern in brackets: s.pop() return s.is_empty() except: return False
def matcher(line): s = Stack() for char in line: if char == "(" or char == "{" or char == "[": s.push(char) elif char == ")" or char == "}" or char == "]": try: if not s.is_empty: return False elif char == ")" and s.top() == "(": s.pop() elif char == "}" and s.top() == "{": s.pop() elif char == "]" and s.top() == "[": s.pop() else: return False except: return False return s.is_empty()