create dynamic array in python code example
Example 1: dynamic array python numpy
from dynarray import DynamicArray
array = DynamicArray()
for element in range(10):
array.append(element)
Example 2: dynamic array logic in python
import ctypes
class DynamicArray(object):
def __init__(self):
self.n = 0
self.capacity = 1
self.A = self.make_array(self.capacity)
def __len__(self):
return self.n
def __getitem__(self, k):
if not 0 <=k <self.n:
return IndexError('k is out of bounds')
return self.A[k]
def append(self, element):
if self.n == self.capacity:
self.resize(2*self.capacity)
self.A[self.n] = element
self.n += 1
def _resize(self, new_cap):
B = self.make_array(new_cap)
for k in range(self.n):
B[k] = self.A[k]
self.A = B
self.capacity = new_cap
def make_array(self,new_cap):
return (new_cap * ctypes.py_object)()
arr = DynamicArray()