Quick actions

cmd+k|ctrl+k

Navigation

Languages

myStack_array

Snippet info

Language

Python

Visibility

public

Author

arthurpc02

Created

2024-10-27T20:01:35.717631Z

Updated

2024-10-27T20:01:35.717631Z

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")
INFO