Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 0

chao_qun_2020/Python

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (12)
master
cclauss-patch-1
mohammadreza490-patch-2
mohammadreza490-patch-1
codespell_--ignore-words-list
Python-3.9rc2
Delete-sleep_sort.py
pytest--durations=10
max-complexityto-15
itsvinayak-patch-1
doubly_linked_list.py-Add-doctests
Fix-ftp-to-allow-doctests
master
Branches (12)
master
cclauss-patch-1
mohammadreza490-patch-2
mohammadreza490-patch-1
codespell_--ignore-words-list
Python-3.9rc2
Delete-sleep_sort.py
pytest--durations=10
max-complexityto-15
itsvinayak-patch-1
doubly_linked_list.py-Add-doctests
Fix-ftp-to-allow-doctests
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (12)
master
cclauss-patch-1
mohammadreza490-patch-2
mohammadreza490-patch-1
codespell_--ignore-words-list
Python-3.9rc2
Delete-sleep_sort.py
pytest--durations=10
max-complexityto-15
itsvinayak-patch-1
doubly_linked_list.py-Add-doctests
Fix-ftp-to-allow-doctests
stack.py 2.64 KB
Copy Edit Raw Blame History
Du Yuanchao authored 2020年09月13日 19:56 +08:00 . Updated Stack (#2414)
class StackOverflowError(BaseException):
pass
class Stack:
"""A stack is an abstract data type that serves as a collection of
elements with two principal operations: push() and pop(). push() adds an
element to the top of the stack, and pop() removes an element from the top
of a stack. The order in which elements come off of a stack are
Last In, First Out (LIFO).
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
"""
def __init__(self, limit: int = 10):
self.stack = []
self.limit = limit
def __bool__(self) -> bool:
return bool(self.stack)
def __str__(self) -> str:
return str(self.stack)
def push(self, data):
""" Push an element to the top of the stack."""
if len(self.stack) >= self.limit:
raise StackOverflowError
self.stack.append(data)
def pop(self):
""" Pop an element off of the top of the stack."""
return self.stack.pop()
def peek(self):
""" Peek at the top-most element of the stack."""
return self.stack[-1]
def is_empty(self) -> bool:
""" Check if a stack is empty."""
return not bool(self.stack)
def is_full(self) -> bool:
return self.size() == self.limit
def size(self) -> int:
""" Return the size of the stack."""
return len(self.stack)
def __contains__(self, item) -> bool:
"""Check if item is in stack"""
return item in self.stack
def test_stack() -> None:
"""
>>> test_stack()
"""
stack = Stack(10)
assert bool(stack) is False
assert stack.is_empty() is True
assert stack.is_full() is False
assert str(stack) == "[]"
try:
_ = stack.pop()
assert False # This should not happen
except IndexError:
assert True # This should happen
try:
_ = stack.peek()
assert False # This should not happen
except IndexError:
assert True # This should happen
for i in range(10):
assert stack.size() == i
stack.push(i)
assert bool(stack) is True
assert stack.is_empty() is False
assert stack.is_full() is True
assert str(stack) == str(list(range(10)))
assert stack.pop() == 9
assert stack.peek() == 8
stack.push(100)
assert str(stack) == str([0, 1, 2, 3, 4, 5, 6, 7, 8, 100])
try:
stack.push(200)
assert False # This should not happen
except StackOverflowError:
assert True # This should happen
assert stack.is_empty() is False
assert stack.size() == 10
assert 5 in stack
assert 55 not in stack
if __name__ == "__main__":
test_stack()
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chaoqun_python/Python.git
git@gitee.com:chaoqun_python/Python.git
chaoqun_python
Python
Python
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /