0% found this document useful (0 votes)
5 views4 pages

Python Fixed and Dynamic Arrays

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Python Fixed and Dynamic Arrays

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

class Array:

def __init__(self, size):


self._size = size
self._data = [None] * size

def __getitem__(self, index):


if not -self._size <= index < self._size:
raise IndexError("Index out of bounds")
if index < 0:
index = self._size + index
return self._data[index]

def __setitem__(self, index, value):


if not -self._size <= index < self._size:
raise IndexError("Index out of bounds")
if index < 0:
index = self._size + index
self._data[index] = value

def __len__(self):
return self._size

def __repr__(self):
return str(self._data)

def __iter__(self):
return iter(self._data)

def __contains__(self, value):


return value in self._data

def fill(self, value):


for i in range(self._size):
self._data[i] = value

def index(self, value):


for i in range(self._size):
if self._data[i] == value:
return i
raise ValueError("Value not found")

def count(self, value):


count = 0
for item in self._data:
if item == value:
count += 1
return count

def reverse(self):
self._data.reverse()

def copy(self):
new_array = Array(self._size)
new_array._data = self._data.copy()
return new_array

def to_list(self):
return self._data.copy()

class DynamicArray:
def __init__(self, capacity=10):
[Link] = capacity
[Link] = 0
self._data = [None] * capacity

def __getitem__(self, index):


if not -[Link] <= index < [Link]:
raise IndexError("Index out of bounds")
if index < 0:
index = [Link] + index
return self._data[index]

def __setitem__(self, index, value):


if not -[Link] <= index < [Link]:
raise IndexError("Index out of bounds")
if index < 0:
index = [Link] + index
self._data[index] = value

def __len__(self):
return [Link]

def __repr__(self):
return str(self._data[:[Link]])

def _resize(self, new_capacity):


new_data = [None] * new_capacity
for i in range([Link]):
new_data[i] = self._data[i]
self._data = new_data
[Link] = new_capacity

def append(self, value):


if [Link] == [Link]:
self._resize(2 * [Link])
self._data[[Link]] = value
[Link] += 1

def insert(self, index, value):


if index < 0 or index > [Link]:
raise IndexError("Index out of bounds")
if [Link] == [Link]:
self._resize(2 * [Link])
for i in range([Link], index, -1):
self._data[i] = self._data[i-1]
self._data[index] = value
[Link] += 1

def pop(self, index=-1):


if [Link] == 0:
raise IndexError("Pop from empty array")
if not -[Link] <= index < [Link]:
raise IndexError("Index out of bounds")
if index < 0:
index = [Link] + index
value = self._data[index]
for i in range(index, [Link] - 1):
self._data[i] = self._data[i+1]
[Link] -= 1
if [Link] < [Link] // 4:
self._resize([Link] // 2)
return value

def remove(self, value):


for i in range([Link]):
if self._data[i] == value:
[Link](i)
return
raise ValueError("Value not found")
# Create a fixed-size array of 5 elements
arr = Array(5)
# Fill the array with values
for i in range(5):
arr[i] = i * 10

print(arr) # Output: [0, 10, 20, 30, 40]

# Access elements
print(arr[2]) # Output: 20
print(arr[-1]) # Output: 40

# Create a dynamic array


dyn_arr = DynamicArray()

# Append elements
for i in range(3):
dyn_arr.append(i * 5)

print(dyn_arr) # Output: [0, 5, 10]

# Insert element
dyn_arr.insert(1, 99)
print(dyn_arr) # Output: [0, 99, 5, 10]

# Remove element
dyn_arr.pop()
print(dyn_arr) # Output: [0, 99, 5]

You might also like