mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 23:50:45 +08:00
Doc snippets for init_linked_list, init_binary_tree, stack_operations and
five list.md entries now reference marked regions of the demo programs
instead of copying code, so the book shows code that is compiled and run.
- Mark regions in the zh demo files and add the snippet comments that the
demos lacked, reusing texts that already have reviewed translations.
- Sync ru by hand, regenerate en/ja/ko from the reviewed dictionaries and
derive zh-hant with OpenCC.
- Convert 97 snippet blocks in six editions to [file]{..}-[region]{..}
references; init_list and the Rust stack block stay literal until their
comments have reviewed translations.
Co-authored-by: Claude Opus 5.5 <[email protected]>
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
"""
|
|
File: stack.py
|
|
Created Time: 2022-11-29
|
|
Author: Peng Chen ([email protected])
|
|
"""
|
|
|
|
"""Driver Code"""
|
|
if __name__ == "__main__":
|
|
# [region]{init_stack}
|
|
# Инициализировать стек
|
|
# В Python нет встроенного класса стека, поэтому list можно использовать как стек
|
|
stack: list[int] = []
|
|
# [endregion]{init_stack}
|
|
|
|
# [region]{push}
|
|
# Помещение элемента в стек
|
|
stack.append(1)
|
|
stack.append(3)
|
|
stack.append(2)
|
|
stack.append(5)
|
|
stack.append(4)
|
|
# [endregion]{push}
|
|
print("Стек stack =", stack)
|
|
|
|
# [region]{peek}
|
|
# Доступ к верхнему элементу стека
|
|
peek: int = stack[-1]
|
|
# [endregion]{peek}
|
|
print("Верхний элемент peek =", peek)
|
|
|
|
# [region]{pop}
|
|
# Извлечение элемента из стека
|
|
pop: int = stack.pop()
|
|
# [endregion]{pop}
|
|
print("Извлеченный элемент pop =", pop)
|
|
print("stack после извлечения =", stack)
|
|
|
|
# [region]{size}
|
|
# Получение длины стека
|
|
size: int = len(stack)
|
|
# [endregion]{size}
|
|
print("Длина стека size =", size)
|
|
|
|
# [region]{is_empty}
|
|
# Проверка на пустоту
|
|
is_empty: bool = len(stack) == 0
|
|
# [endregion]{is_empty}
|
|
print("Пуст ли стек =", is_empty)
|