8000 create array list class · BaseMax/ArrayListPython@ce14a87 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce14a87

Browse files
committed
create array list class
1 parent 9cbda1a commit ce14a87

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

ArrayList.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#
2+
# @Author: Max Base
3+
# @Name: ArrayList Python
4+
# @Repository: https://github.com/BaseMax/ArrayListPython
5+
# @Date: 2022-12-02
6+
#
7+
8+
class ArrayList:
9+
def __init__(self, size=10):
10+
self.size = size
11+
self.count = 0
12+
self.items = []
13+
14+
def is_empty(self):
15+
return self.count == 0
16+
17+
def is_full(self):
18+
return self.count == self.size
19+
20+
def insert(self, item):
21+
if self.is_full():
22+
raise Exception("List is full")
23+
self.items.append(item)
24+
self.count += 1
25+
26+
def remove(self, item):
27+
if self.is_empty():
28+
raise Exception("List is empty")
29+
self.items.remove(item)
30+
self.count -= 1
31+
32+
def search(self, item):
33+
return item in self.items
34+
35+
def get(self, index):
36+
if index < 0 or index >= self.count:
37+
raise Exception("Index out of range")
38+
return self.items[index]
39+
40+
def __str__(self):
41+
return str(self.items)
42+
43+
def __len__(self):
44+
return len(self.items)
45+
46+
def __iter__(self):
47+
return iter(self.items)
48+
49+
def __getitem__(self, index):
50+
return self.get(index)
51+
52+
def __setitem__(self, index, value):
53+
if index < 0 or index >= self.size:
54+
raise Exception("Index out of range")
55+
self.items[index] = value
56+
57+
def __delitem__(self, index):
58+
del self.items[index]
59+

0 commit comments

Comments
 (0)
0