@@ -4,6 +4,145 @@ This is a simple implementation of ArrayList in Python.
4
4
5
5
## Functions
6
6
7
+ <!-- def __init__(self, size=10):
8
+ self.size = size
9
+ self.count = 0
10
+ self.items = []
11
+
12
+ def is_empty(self):
13
+ return self.count == 0
14
+
15
+ def is_full(self):
16
+ return self.count == self.size
17
+
18
+ def add(self, item):
19
+ if self.is_full():
20
+ raise Exception("List is full")
21
+ self.items.append(item)
22
+ self.count += 1
23
+
24
+ def add_at(self, index, item):
25
+ if index < 0 or index >= self.size:
26
+ raise Exception("Index out of range")
27
+ self.items.insert(index, item)
28
+
29
+ def add_first(self, item):
30
+ self.add_at(0, item)
31
+
32
+ def add_last(self, item):
33
+ self.add(item)
34
+
35
+ def remove_at(self, index):
36
+ if index < 0 or index >= self.size:
37
+ raise Exception("Index out of range")
38
+ del self.items[index]
39
+
40
+ def remove_first(self):
41
+ self.remove_at(0)
42
+
43
+ def remove_last(self):
44
+ self.remove_at(self.count - 1)
45
+
46
+ def remove(self):
47
+ self.remove_last()
48
+
49
+ def set(self, index, item):
50
+ if index < 0 or index >= self.size:
51
+ raise Exception("Index out of range")
52
+ if index >= self.count:
53
+ self.count = index + 1
54
+ self[index] = item
55
+
56
+
57
+ def size(self):
58
+ return self.count
59
+
60
+ def clear(self):
61
+ self.items = []
62
+ self.count = 0
63
+
64
+ def to_string(self):
65
+ return str(self.items)
66
+
67
+ def __repr__(self):
68
+ return str(self.items)
69
+
70
+ def __contains__(self, item):
71
+ return self.contains(item)
72
+
73
+ def contains(self, item):
74
+ return item in self.items
75
+
76
+ def index_of(self, item):
77
+ return self.items.index(item)
78
+
79
+ def __eq__(self, other):
80
+ return self.items == other.items
81
+
82
+ def __ne__(self, other):
83
+ return self.items != other.items
84
+
85
+ def __lt__(self, other):
86
+ return self.items < other.items
87
+
88
+ def __le__(self, other):
89
+ return self.items <= other.items
90
+
91
+ def __gt__(self, other):
92
+ return self.items > other.items
93
+
94
+ def __ge__(self, other):
95
+ return self.items >= other.items
96
+
97
+ def __add__(self, other):
98
+ return self.items + other.items
99
+
100
+ def __iadd__(self, other):
101
+ self.items += other.items
102
+ return self
103
+
104
+ def __isub__(self, other):
105
+ self.items -= other.items
106
+ return self
107
+
108
+ def __sub__(self, other):
109
+ return self.items - other.items
110
+
111
+ def __mul__(self, other):
112
+ return self.items * other
113
+
114
+ def __imul__(self, other):
115
+ self.items *= other
116
+ return self
117
+
118
+ def __rmul__(self, other):
119
+ return self.items * other
120
+
121
+ def get(self, index):
122
+ if index < 0 or index >= self.count:
123
+ raise Exception("Index out of range")
124
+ return self.items[index]
125
+
126
+ def __str__(self):
127
+ return str(self.items)
128
+
129
+ def __len__(self):
130
+ return len(self.items)
131
+
132
+ def __iter__(self):
133
+ return iter(self.items)
134
+
135
+ def __getitem__(self, index):
136
+ return self.get(index)
137
+
138
+ def __setitem__(self, index, value):
139
+ if index < 0 or index >= self.size:
140
+ raise Exception("Index out of range")
141
+ self.items[index] = value
142
+
143
+ def __delitem__(self, index):
144
+ del self.items[index] -->
145
+
7
146
- ` add ` - Add an element to the end of the list
8
147
- ` add_at ` - Add an element at a specific index
9
148
- ` remove ` - Remove an element from the list
@@ -17,6 +156,29 @@ This is a simple implementation of ArrayList in Python.
17
156
- ` clear ` - Clear the list
18
157
- ` to_string ` - Get a string representation of the list
19
158
159
+ And a couple of magic methods:
160
+
161
+ - ` __str__ ` - Get a string representation of the list
162
+ - ` __repr__ ` - Get a string representation of the list
163
+ - ` __len__ ` - Get the size of the list
164
+ - ` __iter__ ` - Get an iterator for the list
165
+ - ` __getitem__ ` - Get an element at a specific index
166
+ - ` __setitem__ ` - Set an element at a specific index
167
+ - ` __delitem__ ` - Remove an element at a specific index
168
+ - ` __add__ ` - Add two lists together
169
+ - ` __iadd__ ` - Add two lists together
170
+ - ` __sub__ ` - Subtract two lists
171
+ - ` __isub__ ` - Subtract two lists
172
+ - ` __mul__ ` - Multiply a list by a number
173
+ - ` __imul__ ` - Multiply a list by a number
174
+ - ` __rmul__ ` - Multiply a list by a number
175
+ - ` __eq__ ` - Check if two lists are equal
176
+ - ` __ne__ ` - Check if two lists are not equal
177
+ - ` __lt__ ` - Check if one list is less than another
178
+ - ` __le__ ` - Check if one list is less than or equal to another
179
+ - ` __gt__ ` - Check if one list is greater than another
180
+ - ` __ge__ ` - Check if one list is greater than or equal to another
181
+
20
182
## Usage
21
183
22
184
``` python
0 commit comments