- 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
#
classArrayList:
def__init__(self, size=10):
self.size=size
self.count=0
self.items= []
defis_empty(self):
returnself.count==0
defis_full(self):
returnself.count==self.size
defadd(self, item):
ifself.is_full():
raiseException("List is full")
self.items.append(item)
self.count+=1
defadd_at(self, index, item):
ifindex<0orindex>=self.size:
raiseException("Index out of range")
self.items.insert(index, item)
self.count+=1
defadd_first(self, item):
self.add_at(0, item)
defadd_last(self, item):
self.add(item)
defremove_at(self, index):
ifindex<0orindex>=self.size:
raiseException("Index out of range")
delself.items[index]
self.count-=1
defremove_first(self):
self.remove_at(0)
defremove_last(self):
self.remove_at(self.count-1)
defremove(self):
self.remove_last()
defset(self, index, item):
ifindex<0orindex>=self.size:
raiseException("Index out of range")
ifindex>=self.count:
self.count=index+1
self[index] =item
defsize(self):
returnself.count
defclear(self):
self.items= []
self.count=0
defto_string(self):
returnstr(self.items)
def__repr__(self):
returnstr(self.items)
def__contains__(self, item):
returnself.contains(item)
defcontains(self, item):
returniteminself.items
defindex_of(self, item):
returnself.items.index(item)
def__eq__(self, other):
returnself.items==other.items
def__ne__(self, other):
returnself.items!=other.items
def__lt__(self, other):
returnself.items<other.items
def__le__(self, other):
returnself.items<=other.items
def__gt__(self, other):
returnself.items>other.items
def__ge__(self, other):
returnself.items>=other.items
def__add__(self, other):
returnself.items+other.items
def__iadd__(self, other):
self.items+=other.items
returnself
def__isub__(self, other):
self.items-=other.items
returnself
def__sub__(self, other):
returnself.items-other.items
def__mul__(self, other):
returnself.items*other
def__imul__(self, other):
self.items*=other
returnself
def__rmul__(self, other):
returnself.items*other
defget(self, index):
ifindex<0orindex>=self.count:
raiseException("Index out of range")
returnself.items[index]
def__str__(self):
returnstr(self.items)
def__len__(self):
returnlen(self.items)
def__iter__(self):
returniter(self.items)
def__getitem__(self, index):
returnself.get(index)
def__setitem__(self, index, value):
ifindex<0orindex>=self.size:
raiseException("Index out of range")
self.items[index] =value
def__delitem__(self, index):
delself.items[index]