-
Notifications
You must be signed in to change notification settings - Fork 0
/
imperative_stack.py
82 lines (54 loc) · 1.67 KB
/
imperative_stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Stack implementation using imperative style (only functions)."""
from data_structure.exceptions.collection_exeption import (
CollectionIsEmptyExeption,
)
from data_structure.exceptions.error_messages import stack_is_empty
def make_stack():
"""Return data structure stack.
Returns:
stack data structure
"""
return _make_collection()
def push(stack, element):
"""Add element in stack.
Args:
stack: stack data structure (created by function make_stack())
element: element for added in stack
"""
_insert(stack, element)
def pop(stack):
"""Extract item from stack.
Args:
stack: stack data structure (created by function make_stack())
Returns:
item: top item from stack
Raises:
CollectionIsEmptyExeption: if stack is empty
"""
if is_empty(stack):
raise CollectionIsEmptyExeption(stack_is_empty())
return _extract(stack)
def is_empty(stack):
"""Return True if stack is empty.
Args:
stack: stack data structure (created by function make_stack())
Returns:
size (bool): true if stack is empry, else false
"""
return _size(stack) == 0
def size(stack):
"""Return size of stack.
Args:
stack: stack data structure (created by function make_stack())
Returns:
size (int): stack size
"""
return _size(stack)
def _make_collection():
return []
def _insert(collection, element):
collection.append(element)
def _extract(collection):
return collection.pop()
def _size(collection):
return len(collection)