-
Notifications
You must be signed in to change notification settings - Fork 13
/
customvector.py
140 lines (112 loc) · 3.47 KB
/
customvector.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
class CustomVector:
"""
This is a custom class that implements some operations that are necessary for the access to the elements of a block.
"""
def __init__(self, elements=None):
"""Initialization method.
Parameters:
-----------
elements : :obj:`list` of `str`, optional
A list of elements.
"""
if elements:
self._elements = elements
else:
self._elements = []
def begin(self):
"""This function returns an iterator from the beginning of the list."""
return iter(self._elements)
def rbegin(self):
"""This function returns a reverse iterator from the beginning of the
list."""
rev_list = self._elements[:]
rev_list.reverse()
return iter(rev_list)
def empty(self):
"""This function tells if the list is empty.
Returns
-------
bool
True if empty, False otherwise.
"""
if not self._elements:
return True
return False
def size(self):
"""This function returns the number of elements in the list.
Returns
-------
int
Number of elements.
"""
return len(self._elements)
def operator(self, i):
"""Return element 'x' in the list at index 'i'.
Parameters
----------
i : int
index of the element to return.
Returns
-------
:obj:
element of the list
"""
try:
return self._elements[i]
except IndexError:
raise ("ERROR: Index out of bounds")
def front(self):
"""Returns first element of the list."""
if not self.empty():
return self._elements[0]
else:
return None
def back(self):
"""Returns the last element of the list."""
if not self.empty():
return self._elements[-1]
else:
return None
def pop_back(self):
"""Remove the last element in the list."""
if not self.empty():
return self._elements.pop(-1)
else:
raise ("ERROR: Index out of bounds")
def pop(self):
"""Remove the first element in the list."""
if not self.empty():
return self._elements.pop(0)
else:
raise ("ERROR: Index out of bounds")
def clear(self):
"""Remove all the elements of the list."""
self._elements = []
def push_back(self, e):
"""Insert an element at the end of the list."""
self._elements.append(e)
def insert(self, e, i):
"""Insert an element 'e' at an index 'i'.
Parameters
----------
e : :obj:
Element to insert into the list.
i : int
Index where the element will be inserted.
"""
try:
self._elements.insert(i, e)
except IndexError:
raise ("ERROR: when inserting in index: " + i)
def printer(self):
"""Print the vector."""
print(self._elements)
def return_index(self, index):
"""Returns element at a concrete position in the list."""
return self._elements[index]
def vector(self):
"""Returns all the elements as a list."""
return self._elements
def popAtIndex(self, index):
"""Remove an element at a concrete position in the list."""
return self._elements.pop(index)