Skip to content

Commit 44b8cb0

Browse files
realDuYuanChaogithub-actionscclauss
authored
Updated Stack (#2414)
* * Added type hints * Added test * Formated code * updating DIRECTORY.md * Update stack.py * Test error conditions for pop, peek, and Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent d6bff5c commit 44b8cb0

File tree

2 files changed

+77
-30
lines changed

2 files changed

+77
-30
lines changed

Diff for: DIRECTORY.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* [Sudoku](https://github.com/TheAlgorithms/Python/blob/master/backtracking/sudoku.py)
2525
* [Sum Of Subsets](https://github.com/TheAlgorithms/Python/blob/master/backtracking/sum_of_subsets.py)
2626

27+
## Bit Manipulation
28+
* [Binary Or Operator](https://github.com/TheAlgorithms/Python/blob/master/bit_manipulation/binary_or_operator.py)
29+
2730
## Blockchain
2831
* [Chinese Remainder Theorem](https://github.com/TheAlgorithms/Python/blob/master/blockchain/chinese_remainder_theorem.py)
2932
* [Diophantine Equation](https://github.com/TheAlgorithms/Python/blob/master/blockchain/diophantine_equation.py)
@@ -50,6 +53,7 @@
5053
* [Deterministic Miller Rabin](https://github.com/TheAlgorithms/Python/blob/master/ciphers/deterministic_miller_rabin.py)
5154
* [Diffie](https://github.com/TheAlgorithms/Python/blob/master/ciphers/diffie.py)
5255
* [Elgamal Key Generator](https://github.com/TheAlgorithms/Python/blob/master/ciphers/elgamal_key_generator.py)
56+
* [Enigma Machine2](https://github.com/TheAlgorithms/Python/blob/master/ciphers/enigma_machine2.py)
5357
* [Hill Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/hill_cipher.py)
5458
* [Mixed Keyword Cypher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/mixed_keyword_cypher.py)
5559
* [Morse Code Implementation](https://github.com/TheAlgorithms/Python/blob/master/ciphers/morse_code_implementation.py)
@@ -105,6 +109,7 @@
105109
* [Segment Tree Other](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/segment_tree_other.py)
106110
* [Treap](https://github.com/TheAlgorithms/Python/blob/master/data_structures/binary_tree/treap.py)
107111
* Disjoint Set
112+
* [Alternate Disjoint Set](https://github.com/TheAlgorithms/Python/blob/master/data_structures/disjoint_set/alternate_disjoint_set.py)
108113
* [Disjoint Set](https://github.com/TheAlgorithms/Python/blob/master/data_structures/disjoint_set/disjoint_set.py)
109114
* Hashing
110115
* [Double Hash](https://github.com/TheAlgorithms/Python/blob/master/data_structures/hashing/double_hash.py)
@@ -419,6 +424,7 @@
419424
* [Sum Of Geometric Progression](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_geometric_progression.py)
420425
* [Test Prime Check](https://github.com/TheAlgorithms/Python/blob/master/maths/test_prime_check.py)
421426
* [Trapezoidal Rule](https://github.com/TheAlgorithms/Python/blob/master/maths/trapezoidal_rule.py)
427+
* [Ugly Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/ugly_numbers.py)
422428
* [Volume](https://github.com/TheAlgorithms/Python/blob/master/maths/volume.py)
423429
* [Zellers Congruence](https://github.com/TheAlgorithms/Python/blob/master/maths/zellers_congruence.py)
424430

@@ -474,6 +480,7 @@
474480
* [Sdes](https://github.com/TheAlgorithms/Python/blob/master/other/sdes.py)
475481
* [Sierpinski Triangle](https://github.com/TheAlgorithms/Python/blob/master/other/sierpinski_triangle.py)
476482
* [Tower Of Hanoi](https://github.com/TheAlgorithms/Python/blob/master/other/tower_of_hanoi.py)
483+
* [Triplet Sum](https://github.com/TheAlgorithms/Python/blob/master/other/triplet_sum.py)
477484
* [Two Sum](https://github.com/TheAlgorithms/Python/blob/master/other/two_sum.py)
478485
* [Word Patterns](https://github.com/TheAlgorithms/Python/blob/master/other/word_patterns.py)
479486

@@ -600,6 +607,12 @@
600607
* [Solution42](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_42/solution42.py)
601608
* Problem 43
602609
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_43/sol1.py)
610+
* Problem 44
611+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_44/sol1.py)
612+
* Problem 45
613+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_45/sol1.py)
614+
* Problem 46
615+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_46/sol1.py)
603616
* Problem 47
604617
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_47/sol1.py)
605618
* Problem 48
@@ -608,10 +621,14 @@
608621
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_52/sol1.py)
609622
* Problem 53
610623
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_53/sol1.py)
624+
* Problem 55
625+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_55/sol1.py)
611626
* Problem 551
612627
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py)
613628
* Problem 56
614629
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_56/sol1.py)
630+
* Problem 63
631+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_63/sol1.py)
615632
* Problem 67
616633
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_67/sol1.py)
617634
* Problem 76
@@ -673,7 +690,6 @@
673690
* [Recursive Insertion Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/recursive_insertion_sort.py)
674691
* [Selection Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/selection_sort.py)
675692
* [Shell Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/shell_sort.py)
676-
* [Sleep Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/sleep_sort.py)
677693
* [Stooge Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/stooge_sort.py)
678694
* [Strand Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/strand_sort.py)
679695
* [Tim Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/tim_sort.py)
@@ -685,6 +701,7 @@
685701
## Strings
686702
* [Aho-Corasick](https://github.com/TheAlgorithms/Python/blob/master/strings/aho-corasick.py)
687703
* [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py)
704+
* [Capitalize](https://github.com/TheAlgorithms/Python/blob/master/strings/capitalize.py)
688705
* [Check Anagrams](https://github.com/TheAlgorithms/Python/blob/master/strings/check_anagrams.py)
689706
* [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py)
690707
* [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/is_palindrome.py)
@@ -715,6 +732,7 @@
715732
* [Emails From Url](https://github.com/TheAlgorithms/Python/blob/master/web_programming/emails_from_url.py)
716733
* [Fetch Bbc News](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_bbc_news.py)
717734
* [Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_github_info.py)
735+
* [Fetch Jobs](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_jobs.py)
718736
* [Get Imdb Top 250 Movies Csv](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdb_top_250_movies_csv.py)
719737
* [Get Imdbtop](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdbtop.py)
720738
* [Slack Message](https://github.com/TheAlgorithms/Python/blob/master/web_programming/slack_message.py)

Diff for: data_structures/stacks/stack.py

+58-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
__author__ = "Omkar Pathak"
1+
class StackOverflowError(BaseException):
2+
pass
23

34

45
class Stack:
@@ -7,18 +8,17 @@ class Stack:
78
element to the top of the stack, and pop() removes an element from the top
89
of a stack. The order in which elements come off of a stack are
910
Last In, First Out (LIFO).
10-
1111
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
1212
"""
1313

14-
def __init__(self, limit=10):
14+
def __init__(self, limit: int = 10):
1515
self.stack = []
1616
self.limit = limit
1717

18-
def __bool__(self):
18+
def __bool__(self) -> bool:
1919
return bool(self.stack)
2020

21-
def __str__(self):
21+
def __str__(self) -> str:
2222
return str(self.stack)
2323

2424
def push(self, data):
@@ -29,21 +29,20 @@ def push(self, data):
2929

3030
def pop(self):
3131
""" Pop an element off of the top of the stack."""
32-
if self.stack:
33-
return self.stack.pop()
34-
else:
35-
raise IndexError("pop from an empty stack")
32+
return self.stack.pop()
3633

3734
def peek(self):
3835
""" Peek at the top-most element of the stack."""
39-
if self.stack:
40-
return self.stack[-1]
36+
return self.stack[-1]
4137

42-
def is_empty(self):
38+
def is_empty(self) -> bool:
4339
""" Check if a stack is empty."""
4440
return not bool(self.stack)
4541

46-
def size(self):
42+
def is_full(self) -> bool:
43+
return self.size() == self.limit
44+
45+
def size(self) -> int:
4746
""" Return the size of the stack."""
4847
return len(self.stack)
4948

@@ -52,24 +51,54 @@ def __contains__(self, item) -> bool:
5251
return item in self.stack
5352

5453

55-
class StackOverflowError(BaseException):
56-
pass
57-
54+
def test_stack() -> None:
55+
"""
56+
>>> test_stack()
57+
"""
58+
stack = Stack(10)
59+
assert bool(stack) is False
60+
assert stack.is_empty() is True
61+
assert stack.is_full() is False
62+
assert str(stack) == "[]"
63+
64+
try:
65+
_ = stack.pop()
66+
assert False # This should not happen
67+
except IndexError:
68+
assert True # This should happen
69+
70+
try:
71+
_ = stack.peek()
72+
assert False # This should not happen
73+
except IndexError:
74+
assert True # This should happen
5875

59-
if __name__ == "__main__":
60-
stack = Stack()
6176
for i in range(10):
77+
assert stack.size() == i
6278
stack.push(i)
6379

64-
print("Stack demonstration:\n")
65-
print("Initial stack: " + str(stack))
66-
print("pop(): " + str(stack.pop()))
67-
print("After pop(), the stack is now: " + str(stack))
68-
print("peek(): " + str(stack.peek()))
80+
assert bool(stack) is True
81+
assert stack.is_empty() is False
82+
assert stack.is_full() is True
83+
assert str(stack) == str(list(range(10)))
84+
assert stack.pop() == 9
85+
assert stack.peek() == 8
86+
6987
stack.push(100)
70-
print("After push(100), the stack is now: " + str(stack))
71-
print("is_empty(): " + str(stack.is_empty()))
72-
print("size(): " + str(stack.size()))
73-
num = 5
74-
if num in stack:
75-
print(f"{num} is in stack")
88+
assert str(stack) == str([0, 1, 2, 3, 4, 5, 6, 7, 8, 100])
89+
90+
try:
91+
stack.push(200)
92+
assert False # This should not happen
93+
except StackOverflowError:
94+
assert True # This should happen
95+
96+
assert stack.is_empty() is False
97+
assert stack.size() == 10
98+
99+
assert 5 in stack
100+
assert 55 not in stack
101+
102+
103+
if __name__ == "__main__":
104+
test_stack()

0 commit comments

Comments
 (0)