-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_types.py
125 lines (107 loc) · 4.32 KB
/
data_types.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class Stack:
""" A variable-length LIFO data structure where each item is added to the
top of the stack."""
def __init__(self, *args):
""" The constructor for a stack.
Inputs: Takes any number of any items to put in the stack.
Outputs: None."""
self.items = [*args]
@property
def is_empty(self):
""" A property that returns a Boolean value detailing whether the stack
is empty (it has no items)."""
return len(self.items) == 0
def __len__(self):
""" A method that returns the length of the stack (an integer)."""
return self.items.__len__()
def push(self, item):
""" This method pushes a new item to the top of the stack, so that it
will be the last item in.
Inputs: item (any object that is to be pushed to the stack).
Outputs: None."""
self.items.append(item)
def pop(self):
""" This method pops an item from the stack, removing it from the stack
and returning it.
Inputs: None.
Outputs: The item that was currently occupying the last (top)
space in the stack."""
if len(self.items) == 0:
return None
else:
last_item = self.items[-1]
self.items = self.items[:-1]
return last_item
def remove(self):
""" This method removes an item from the top of the stack, removing it
from the stack and not returning it.
Inputs: None.
Outputs: None."""
if len(self.items) != 0:
self.items = self.items[:-1]
def peek(self):
""" This method peeks at the top item of the stack, returning it but not
removing it from the stack.
Inputs: None.
Outputs: The item that was currently occupying the last (top)
space in the stack."""
if len(self.items) > 0:
return self.items[-1]
else:
return None
def clear(self):
""" This method completely clears the stack, removing all items from it.
Inputs: None.
Outputs: None."""
self.items = []
class Queue:
""" A variable-length FIFO data structure where each item is added to the
back of the queue."""
def __init__(self, *args):
""" The constructor for the Queue class.
Inputs: Takes any number of any objects to put in the queue.
Outputs: None."""
self.items = [*args]
def __len__(self):
""" A method that returns the length of the queue (an integer)."""
return self.items.__len__()
def enqueue(self, item):
""" This method is used to add an item to the end / back of a queue.
Inputs: Any item / object to be added to the end of the queue.
Outputs: None."""
self.items.append(item)
def dequeue(self):
""" This method is used to remove and return an item from the front of
the queue.
Inputs: None.
Outputs: The item that was occupying the front place in the queue.
"""
if len(self.items) == 0:
return None
else:
return self.items.pop(0)
@property
def is_empty(self):
""" A property that returns a Boolean value detailing whether the queue
is empty (it has no items)."""
return len(self.items) == 0
def clear(self):
""" This method completely clears the queue, removing all items from it.
Inputs: None.
Outputs: None."""
self.items = []
def remove(self):
""" This method removes the first item in the queue and does not return
it to the user.
Inputs: None.
Outputs: None."""
self.items = self.items[1:]
def peek(self):
""" This method peeks at the first item in the queue without actually
removing it from the queue.
Inputs: None.
Outputs: The item that was occupying the front place in the queue.
"""
if len(self.items) == 0:
return None
return self.items[-1]