Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack – Data Structure. Stack Definition class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item):

Similar presentations


Presentation on theme: "Stack – Data Structure. Stack Definition class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item):"— Presentation transcript:

1 Stack – Data Structure

2 Stack Definition class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items)-1] def size(self): return len(self.items)

3 Stack Test #from Stack import Stack import Stack s=Stack.Stack() print(s.isEmpty()) s.push(4) s.push('dog') print(s.peek()) s.push(True) print(s.size()) print(s.isEmpty()) s.push(8.4) print(s.pop()) print(s.size())

4 output True dog 3 False 8.4 True 2

5 Balanced bracket checker from Stack import Stack def parChecker(symbolString): s = Stack() balanced = True index = 0 while index < len(symbolString) and balanced: symbol = symbolString[index] if symbol == "(": s.push(symbol) else: if s.isEmpty(): balanced = False else: s.pop() index = index + 1 if balanced and s.isEmpty(): return True else: return False

6 test print(parChecker('((()))')) print(parChecker('(()')) Outputs True False


Download ppt "Stack – Data Structure. Stack Definition class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item):"

Similar presentations


Ads by Google