Compare commits

..

7 Commits

Author SHA1 Message Date
05d5b66dec added more tests 2026-03-15 16:32:07 -04:00
d876bc9a73 added tests 2026-03-15 16:31:40 -04:00
9e8153eb1f added is_empty 2026-03-15 16:30:12 -04:00
b2ecff6a6b added peek 2026-03-15 16:29:43 -04:00
e25318b336 added push 2026-03-15 16:28:26 -04:00
bb366686c1 added __len__ 2026-03-15 16:28:03 -04:00
5b3f9eff6b added __init__ 2026-03-15 16:27:32 -04:00

View File

@@ -19,7 +19,9 @@ class Stack[T]:
def __init__(self): def __init__(self):
pass ''' initializes an empty stack '''
self._data = LinkedList()
def __len__(self) -> int: def __len__(self) -> int:
''' allows the len function to be called using an ArrayStack object, e.g., ''' allows the len function to be called using an ArrayStack object, e.g.,
stack = ArrayStack() stack = ArrayStack()
@@ -27,7 +29,7 @@ class Stack[T]:
Returns: Returns:
number of elements in the stack, as an integer number of elements in the stack, as an integer
''' '''
pass return len(self._data)
def push(self, item: T) -> None: def push(self, item: T) -> None:
''' pushes a given item of arbitrary type onto the stack ''' pushes a given item of arbitrary type onto the stack
@@ -36,7 +38,7 @@ class Stack[T]:
Returns: Returns:
None None
''' '''
pass self._data.insert_head(item)
def pop(self) -> T: def pop(self) -> T:
''' removes the topmost element from the stack and returns that element ''' removes the topmost element from the stack and returns that element
@@ -45,7 +47,9 @@ class Stack[T]:
Raises: Raises:
EmptyError exception if the stack is empty EmptyError exception if the stack is empty
''' '''
pass if self.is_empty():
raise EmptyError("Stack is empty")
return self._data.remove_head()
def peek(self) -> T: def peek(self) -> T:
''' returns the topmost element from the stack without modifying the stack ''' returns the topmost element from the stack without modifying the stack
@@ -54,14 +58,16 @@ class Stack[T]:
Raises: Raises:
EmptyError exception if the stack is empty EmptyError exception if the stack is empty
''' '''
pass if self.is_empty():
raise EmptyError("Stack is empty")
return self._data.get_head()
def is_empty(self) -> bool: def is_empty(self) -> bool:
''' indicates whether the stack is empty ''' indicates whether the stack is empty
Returns: Returns:
True if the stack is empty, False otherwise True if the stack is empty, False otherwise
''' '''
pass return len(self._data) == 0
def __str__(self) -> str: def __str__(self) -> str:
''' returns an str implementation of the Stack ''' ''' returns an str implementation of the Stack '''
@@ -69,8 +75,39 @@ class Stack[T]:
return str(self._data) return str(self._data)
def main(): def main():
# add your tests here! '''test functions'''
pass s = Stack() # I added this so stack doesnt need to be reinitialized for each test
assert len(s) == 0
assert s.is_empty() is True
s.push(10)
assert len(s) == 1
assert s.peek() == 10
s.push(20)
s.push(30)
assert len(s) == 3
assert s.peek() == 30
assert s.pop() == 30
assert s.pop() == 20
assert s.pop() == 10
assert s.is_empty() is True
try:
s.pop()
assert False
except EmptyError:
pass
try:
s.peek()
assert False
except EmptyError:
pass
print("All Stack tests passed.") #only runs if all previous tests pass
if __name__ == "__main__": if __name__ == "__main__":
main() main()