-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayList.py
148 lines (108 loc) · 3.6 KB
/
ArrayList.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
#
# @Author: Max Base
# @Name: ArrayList Python
# @Repository: https://github.com/BaseMax/ArrayListPython
# @Date: 2022-12-02
#
class ArrayList:
def __init__(self, size=10):
self.size = size
self.count = 0
self.items = []
def is_empty(self):
return self.count == 0
def is_full(self):
return self.count == self.size
def add(self, item):
if self.is_full():
raise Exception("List is full")
self.items.append(item)
self.count += 1
def add_at(self, index, item):
if index < 0 or index >= self.size:
raise Exception("Index out of range")
self.items.insert(index, item)
self.count += 1
def add_first(self, item):
self.add_at(0, item)
def add_last(self, item):
self.add(item)
def remove_at(self, index):
if index < 0 or index >= self.size:
raise Exception("Index out of range")
del self.items[index]
self.count -= 1
def remove_first(self):
self.remove_at(0)
def remove_last(self):
self.remove_at(self.count - 1)
def remove(self):
self.remove_last()
def set(self, index, item):
if index < 0 or index >= self.size:
raise Exception("Index out of range")
if index >= self.count:
self.count = index + 1
self[index] = item
def size(self):
return self.count
def clear(self):
self.items = []
self.count = 0
def to_string(self):
return str(self.items)
def __repr__(self):
return str(self.items)
def __contains__(self, item):
return self.contains(item)
def contains(self, item):
return item in self.items
def index_of(self, item):
return self.items.index(item)
def __eq__(self, other):
return self.items == other.items
def __ne__(self, other):
return self.items != other.items
def __lt__(self, other):
return self.items < other.items
def __le__(self, other):
return self.items <= other.items
def __gt__(self, other):
return self.items > other.items
def __ge__(self, other):
return self.items >= other.items
def __add__(self, other):
return self.items + other.items
def __iadd__(self, other):
self.items += other.items
return self
def __isub__(self, other):
self.items -= other.items
return self
def __sub__(self, other):
return self.items - other.items
def __mul__(self, other):
return self.items * other
def __imul__(self, other):
self.items *= other
return self
def __rmul__(self, other):
return self.items * other
def get(self, index):
if index < 0 or index >= self.count:
raise Exception("Index out of range")
return self.items[index]
def __str__(self):
return str(self.items)
def __len__(self):
return len(self.items)
def __iter__(self):
return iter(self.items)
def __getitem__(self, index):
return self.get(index)
def __setitem__(self, index, value):
if index < 0 or index >= self.size:
raise Exception("Index out of range")
self.items[index] = value
def __delitem__(self, index):
del self.items[index]