8000 Adding more to class · BaseMax/ArrayListPython@034b001 · GitHub
[go: up one dir, main page]

Skip to content

Commit 034b001

Browse files
committed
Adding more to class
1 parent 2c8f063 commit 034b001

File tree

1 file changed

+95
-19
lines changed

1 file changed

+95
-19
lines changed

ArrayList.py

Lines changed: 95 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@
66
#
77

88
class ArrayList:
9-
# - `add` - Add an element to the end of the list
10-
# - `add_at` - Add an element at a specific index
11-
# - `remove` - Remove an element from the list
12-
# - `remove_at` - Remove an element at a specific index
13-
# - `get` - Get an element at a specific index
14-
# - `set` - Set an element at a specific index
15-
# - `size` - Get the size of the list
16-
# - `is_empty` - Check if the list is empty
17-
# - `contains` - Check if the list contains an element
18-
# - `index_of` - Get the index of an element
19-
# - `clear` - Clear the list
20-
# - `to_string` - Get a string representation of the list
219
def __init__(self, size=10):
2210
self.size = size
2311
self.count = 0
@@ -29,20 +17,108 @@ def is_empty(self):
2917
def is_full(self):
3018
return self.count == self.size
3119

32-
def insert(self, item):
20+
def add(self, item):
3321
if self.is_full():
3422
raise Exception("List is full")
3523
self.items.append(item)
3624
self.count += 1
25+
26+
def add_at(self, index, item):
27+
if index < 0 or index >= self.size:
28+
raise Exception("Index out of range")
29+
self.items.insert(index, item)
30+
31+
def add_first(self, item):
32+
self.add_at(0, item)
33+
34+
def add_last(self, item):
35+
self.add(item)
36+
37+
def remove_at(self, index):
38+
if index < 0 or index >= self.size:
39+
raise Exception("Index out of range")
40+
del self.items[index]
41+
42+
def remove_first(self):
43+
self.remove_at(0)
44+
45+
def remove_last(self):
46+
self.remove_at(self.count - 1)
47+
48+
def remove(self):
49+
self.remove_last()
50+
51+
def set(self, index, item):
52+
if index < 0 or index >= self.size:
53+
raise Exception("Index out of range")
54+
if index >= self.count:
55+
self.count = index + 1
56+
self[index] = item
57+
58+
59+
def size(self):
60+
return self.count
61+
62+
def clear(self):
63+
self.items = []
64+
self.count = 0
3765

38-
def remove(self, item):
39-
if self.is_empty():
40-
raise Exception("List is empty")
41-
self.items.remove(item)
42-
self.count -= 1
66+
def to_string(self):
67+
return str(self.items)
4368

44-
def search(self, item):
69+
def __repr__(self):
70+
return str(self.items)
71+
72+
def __contains__(self, item):
73+
return self.contains(item)
74+
75+
def contains(self, item):
4576
return item in self.items
77+
78+
def index_of(self, item):
79+
return self.items.index(item)
80+
81+
def __eq__(self, other):
82+
return self.items == other.items
83+
84+
def __ne__(self, other):
85+
return self.items != other.items
86+
87+
def __lt__(self, other):
88+
return self.items < other.items
89+
90+
def __le__(self, other):
91+
return self.items <= other.items
92+
93+
def __gt__(self, other):
94+
return self.items > other.items
95+
96+
def __ge__(self, other):
97+
return self.items >= other.items
98+
99+
def __add__(self, other):
100+
return self.items + other.items
101+
102+
def __iadd__(self, other):
103+
self.items += other.items
104+
return self
105+
106+
def __isub__(self, other):
107+
self.items -= other.items
108+
return self
109+
110+
def __sub__(self, other):
111+
return self.items - other.items
112+
113+
def __mul__(self, other):
114+
return self.items * other
115+
116+
def __imul__(self, other):
117+
self.items *= other
118+
return self
119+
120+
def __rmul__(self, other):
121+
return self.items * other
46122

47123
def get(self, index):
48124
if index < 0 or index >= self.count:

0 commit comments

Comments
 (0)
0