This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interfaces.py
226 lines (201 loc) · 7.52 KB
/
Interfaces.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from abc import abstractmethod
import sys
# THIS FILE WAS CREATED BY DR. OSCAR PONCE FOR CECS 274 @ CSULB
'''
Abstract Data types: It provides the list of interfaces
(Stack, Queue, Deque, List, Set)
'''
class Queue:
'''
Queue: The Queue interface represents a collection of elements to which we can
add elements and remove the next element.
'''
@abstractmethod
def add(self, x : object) :
'''
add: Add the value x to the Queue
Inputs:
x: Object type, i.e., any object
'''
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def remove(self) -> object:
'''
remove: remove the next (previously added) value, y, from the
Queue and return y. The Queue’s queueing discipline
decides which element should be removed.
Return: Object type
'''
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def size(self) -> int:
'''
size: Returns the size of the queue
Return: an integer greater or equal to zero representing the number
of elements in the queue
'''
raise NotImplementedError("Subclasses should implement this!")
def __len__(self) :
'''
__len__: Returns the size of the queue when using len, i.e., len(q)
Return: an integer greater or equal to zero representing the number
of elements in the queue
'''
return self.size()
class Stack:
'''
Stack: It is a LIFO Queue, the most recently added element
is the next one removed.
'''
@abstractmethod
def push(self, x : object) :
'''
push: Insert an element in the tail of the stack
Inputs:
x: Object type, i.e., any object
'''
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def pop(self) -> object:
'''
pop: Remove the last element in the stack
Returns: the object of the tail if it is no empty
'''
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def size(self) -> int:
'''
size: Returns the size of the stack
Return: an integer greater or equal to zero representing the number
of elements in the stack
'''
raise NotImplementedError("Subclasses should implement this!")
def __len__(self) :
'''
__len__: Returns the size of the queue when using len, i.e., len(s)
where s is a stack instance
Return: an integer greater or equal to zero representing the number
of elements in the stack
'''
return self.size()
class List:
'''
List: is the abstract data type of a List. The behavior depends on
the implementation
'''
@abstractmethod
def add(self, i : int, x : object) :
'''
add: shift one position all the items j>=i, insert an element
at position i of the list and increment the number of elements
in the list
Inputs:
i: Index that is integer non negative and at most n
x: Object type, i.e., any object
'''
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def set(self, i : int, x : object) :
'''
Set: Set the value x in the index i of the list
Inputs:
i: Index that is integer non negative and at most n
x: Object type, i.e., any object
'''
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def get(self, i : int) -> object:
'''
get: returns the value x in the index i of the list
Inputs:
i: Index that is integer non negative and at most n
Return: return the value x in the index i of the list
'''
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def remove(self, i : int) -> object:
'''
remove: remove th element i and shift all the elements j > i
one position to the left and decrease n
Return: return the value x to be remove.
'''
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def size(self) -> int:
'''
size: Returns the size of the list
Return: an integer greater or equal to zero representing the number
of elements in the list
'''
raise NotImplementedError("Subclasses should implement this!")
def __iter__(self):
'''
__iter__: Initialize the iterator which is used in the for loops
'''
raise NotImplementedError("Subclasses should implement this!")
def __next__(self):
'''
__next__: Move to the next itme in the iterator. It is used in the for loops
'''
raise NotImplementedError("Subclasses should implement this!")
def __len__(self) -> int:
'''
__len__: Returns the size of the queue when using len, i.e., len(l)
where l is a list instance
Return: an integer greater or equal to zero representing the number
of elements in the queue
'''
return self.size()
def __getitem__(self, i) -> object:
'''
__getitem__: Returns the item in the position i in array format, i.e., l[i]
where l is a list instance
Input:
i: positive integer less than n
Return: the item at index i
'''
if isinstance(i, slice):
return [self.get(i) for i in range(i.start, i.stop)]
else:
return self.get(i)
def __setitem__(self, i :int, x : object):
'''
__setitem__: Sets the item x in the index in array format, i.e., l[i] = x
where l is a list instance
Input:
i: positive integer less than or equal n
if i in [0, n) then it sets x at index i
if i = n then it adds x at the end
x: the item to set
'''
if i == self.size() :
self.add(i, x)
else:
self.set(i, x)
class Graph:
'''
Graph: It is the abstract data type of a graph.
'''
@abstractmethod
def add_edge(self, i : int, j: int) :
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def remove_edge(self, i : int, j: int) :
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def has_edge(self, i : int, j: int) -> bool:
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def out_edges(self, i : int) -> List:
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def in_edges(self, j: int) -> List:
raise NotImplementedError("Subclasses should implement this!")
@abstractmethod
def size(self) -> int:
'''
size: Returns the size of the set
Return: an integer greater or equal to zero representing the number
of elements in the set
'''
raise NotImplementedError("Subclasses should implement this!")