-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARRAY.py
44 lines (37 loc) · 1.16 KB
/
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
41
42
43
44
import ctypes
class Arr:
__slots__="size","capacity","array"
def __init__(self):
self.capacity=2
self.array=(ctypes.py_object*self.capacity)()
self.size=0
def append(self,data):
if self.size==self.capacity:
self.resize(2*self.capacity)
self.array[self.size]=data
self.size+=1
def __len__(self):
return self.size
def resize(self,newcap):
newarray=(ctypes.py_object*newcap)()
for i in range(self.size):
newarray[i]=self.array[i]
self.array=newarray
self.capacity=newcap
def is_empty(self):
return self.size==0
def __getitem__(self,index):
if not self.is_empty():
if 0<=index<self.size:
return self.array[index]
raise IndexError('invalis index')
return "Array is empty"
def __str__(self):
s="["
for i in range(self.size):
s+=str(self.array[i])+","
s=s.rstrip(",")+"]"
return s
def __iter__(self):
for i in range(1,self.size):
yield self.array[i]