myStack_array

Run Settings
LanguagePython
Language Version
Run Command
class Stack: def __init__(self): self.array = [] self.length = 0 def peek(self): if self.isEmpty(): raise Exception("Stack is empty.") else: return self.array[self.length-1] def push(self, value): self.array.append(value) self.length+=1 def pop(self): if self.isEmpty(): raise Exception("Stack is empty.") else: returnVal = self.array[self.length-1] self.length-=1 return returnVal def isEmpty(self): return (True if self.length == 0 else False) print("Hello World!") myStack = Stack() print(myStack.isEmpty()) assert(myStack.isEmpty() == True) myStack.push(5) myStack.push(8) assert(myStack.isEmpty() == False) assert(myStack.peek()==8) print(myStack.pop()) assert(myStack.peek()==5) print(myStack.pop()) assert(myStack.isEmpty() == True) try: print(myStack.pop()) except Exception as e: print(f"exception: {e}") print("Goodbye Cruel World")
Editor Settings
Theme
Key bindings
Full width
Lines