-
Notifications
You must be signed in to change notification settings - Fork 62
/
array.py
40 lines (30 loc) · 945 Bytes
/
array.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
# 动态数组的实现
class Array:
def __init__(self, capacity: int):
self.capacity = capacity
self.data = []
def find(self, idx: int):
if idx >= len(self.data):
raise IndexError("invalid key {}".format(idx))
return self.data[idx]
def add(self, val) -> bool:
if len(self.data) == self.capacity:
return False
self.data.append(val)
return True
def set(self, idx: int, val):
self.data.insert(idx, val)
def delete(self, idx: int):
if idx >= len(self.data):
raise IndexError("invalid key {}".format(idx))
self.data.pop(idx)
def size(self) -> int:
return len(self.data)
def print(self):
for n in self.data:
print(n, end=" ")
def __iter__(self):
for item in self.data:
yield item
def __getitem__(self, item):
return self.data[item]