mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-30 19:02:46 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -6,18 +6,18 @@ Author: Peng Chen ([email protected])
|
||||
|
||||
|
||||
class ArrayStack:
|
||||
"""Stack based on array implementation"""
|
||||
"""Stack implemented with an array"""
|
||||
|
||||
def __init__(self):
|
||||
"""Constructor"""
|
||||
self._stack: list[int] = []
|
||||
|
||||
def size(self) -> int:
|
||||
"""Get the length of the stack"""
|
||||
"""Get the stack size"""
|
||||
return len(self._stack)
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""Check if the stack is empty"""
|
||||
"""Check whether the stack is empty"""
|
||||
return self.size() == 0
|
||||
|
||||
def push(self, item: int):
|
||||
@@ -31,42 +31,42 @@ class ArrayStack:
|
||||
return self._stack.pop()
|
||||
|
||||
def peek(self) -> int:
|
||||
"""Access top of the stack element"""
|
||||
"""Access the top element"""
|
||||
if self.is_empty():
|
||||
raise IndexError("Stack is empty")
|
||||
return self._stack[-1]
|
||||
|
||||
def to_list(self) -> list[int]:
|
||||
"""Return list for printing"""
|
||||
"""Return a list for printing"""
|
||||
return self._stack
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize stack
|
||||
# Initialize the stack
|
||||
stack = ArrayStack()
|
||||
|
||||
# Elements push onto stack
|
||||
# Push elements onto the stack
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
print("stack =", stack.to_list())
|
||||
print("Stack stack =", stack.to_list())
|
||||
|
||||
# Access top of the stack element
|
||||
# Access the top element
|
||||
peek: int = stack.peek()
|
||||
print("Top of the stack element peek =", peek)
|
||||
print("Top element peek =", peek)
|
||||
|
||||
# Element pop from stack
|
||||
# Pop an element from the stack
|
||||
pop: int = stack.pop()
|
||||
print("Popped element pop =", pop)
|
||||
print("After pop stack =", stack.to_list())
|
||||
print("After popping, stack =", stack.to_list())
|
||||
|
||||
# Get the length of the stack
|
||||
# Get the stack size
|
||||
size: int = stack.size()
|
||||
print("Length of the stack size =", size)
|
||||
print("Stack size =", size)
|
||||
|
||||
# Check if it is empty
|
||||
# Check whether it is empty
|
||||
is_empty: bool = stack.is_empty()
|
||||
print("Is the stack empty =", is_empty)
|
||||
|
||||
Reference in New Issue
Block a user