Skip to content

Commit 0df3f1b

Browse files
committed
Made exception private in stack.py
Added function to empty the stack
1 parent 4bbf321 commit 0df3f1b

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

stack.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Stack:
2-
class EmptyStackError(Exception):
2+
class __EmptyStackError(Exception):
33
def __str__(self) -> str:
44
return "Stack is empty."
55

@@ -21,7 +21,7 @@ def is_empty(self) -> bool:
2121

2222
def top(self) -> object:
2323
if self.is_empty():
24-
raise Stack.EmptyStackError
24+
raise Stack.__EmptyStackError
2525
return self._data[-1]
2626

2727
def push(self, element: object) -> None:
@@ -30,21 +30,25 @@ def push(self, element: object) -> None:
3030

3131
def pop(self) -> object:
3232
if self.is_empty():
33-
raise Stack.EmptyStackError
33+
raise Stack.__EmptyStackError
3434
self._size -= 1
3535
return self._data.pop()
3636

37+
def empty_stack(self) -> None:
38+
while not self.is_empty():
39+
self.pop()
40+
3741

3842
if __name__ == '__main__':
3943
stack = Stack()
4044
print(stack.is_empty())
4145
for i in range(10):
4246
stack.push(i)
4347
print(stack.top())
48+
print(stack.pop())
4449
print(stack)
4550
print(len(stack))
4651
print(stack.length())
4752
print(stack.is_empty())
48-
for i in range(10):
49-
print(stack.pop())
53+
stack.empty_stack()
5054
print(stack.is_empty())

0 commit comments

Comments
 (0)