Question
Modify your list implementation so that the size of the underlying array is dynamic. The base size of the array is 20 and should never
Modify your list implementation so that the size of the underlying array is dynamic. The base size of the array is 20 and should never be less than 20. However, if the list becomes full, it is resized to be 2 times larger than the current size. Likewise, the underlying size should decrease by half if the underlying array is larger than the base size but the content occupies less than 1 of the available space. When resizing the 1/8 list, retain the contents of the list. That is, when it is initially filled, it will be resized to 20 items, then 40,while retaining the contents initially in it. The same happens when the size of the array shrinks. MY CODE ARE IN PYTHON
My code is :
and code for
referential_array can get at https://repl.it/@MuhammadFermi/week6-1 or
import ctypes def build_array(size): if size <= 0: raise ValueError("Array size should be larger than 0.") if not isinstance(size, int): raise ValueError("Array size should be an integer.") array = (size * ctypes.py_object)() array[:] = size * [None] return array
from referential_array import build_array class ArrayList: Array = None size = 0 def __init__(self, size): self.Array = build_array(size) self.size = size def __str__(self): string = "" for i in self.Array: if (i != None): string = string + str(i) + " " return string def __len__(self): return self.size def __contains__(self, item): for i in self.Array: if i == item: return True return False def __getitem__(self, index): index = int(index) if (index > -self.size and index < self.size): if (index < 0): return (self.Array[self.size + index]) else: return (self.Array[index]) else: raise ValueError("Index is out of range") def __setitem__(self, index, item): index = int(index) if (index > -self.size and index < self.size): if (index < 0): self.Array[self.size + index] = item else: self.Array[index] = item else: raise ValueError("Index is out of range") def __eq__(self, other): return self == other def append(self, item): for i in range(0, self.size): if (self.Array[i] == None): self.Array[i] = item return raise ValueError("List is full") def insert(self, index, item): index = int(index) if (index > -self.size and index < self.size): if (index < 0): self.Array[self.size + index - 1] = item else: self.Array[index - 1] = item else: raise ValueError("Index is out of range") def remove(self, item): for i in self.Array: if (i == item): self.Array[i] = None return raise ValueError("List is empty") def delete(self, index): index = int(index) if (index > -self.size and index < self.size): if (index < 0): for i in range(self.size + index, self.size - 1): self.Array[i] = self.Array[i + 1] else: for i in range(index, self.size - 1): self.Array[i] = self.Array[i + 1] else: raise ValueError("Index is out of range") |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started