Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a class called DynamicArray given below and I need help giving an implementation of a function called remove_all(data, value) that removes all occurrences

I have a class called DynamicArray given below and I need help giving an implementation of a function called remove_all(data, value) that removes all occurrences of value from the given list, such that the worst-case running time of the function is O(n) on a list with n elements. I also incldued a function called remove(only removes first occurance of an element not all occurances) that maybe you can edit to fit the requirements of remove_all function. Please give an implementation file as well and please type and show terminal execution or shell execution. python***

import ctypes

class DynamicArray: """A dynamic array class akin to a simplified Python list."""

def__init__(self): """create an empty array.""" self._n = 0 #count actual elements self._capacity = 1 #default array capacity self._A = self._make_array(self._capacity) #low level array

def __len__(self) """Return number of elements stored in the array""" return slef._n

def__getitem__(self,k): """Return element at index k.""" if not 0<=k < self._n: raise IndexError('invalid index') return self._A[k] #retrieve from array

def append(self,obj): """Add object to end of the array.""" if self._n == self._capacity: self._resize(2 * self._capacity) self._A[self. n] = obj self._n += 1

def resize(self, c): """Resize internal array to capacity c.""" B = self._make array(c) for k in range(self. n): B[k] = self._A[k] self._A = B self._capacity = c

def make array(self, c): """Return new array with capacity c.""" return (c*ctypes.py_object)() def remove(self, value): """Remove first occurrence of value (or raise ValueError).""" for k in range(self._n): if self._A[k] == value: for j in range(k, self._n 1): self._A[j] = self._A[j+1] self._A[self._n 1] = None self._n = 1 return raise ValueError('value not found')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How to solve maths problems with examples

Answered: 1 week ago

Question

Explain Coulomb's law with an example

Answered: 1 week ago

Question

What is operating system?

Answered: 1 week ago

Question

What is Ohm's law and also tell about Snell's law?

Answered: 1 week ago

Question

1. How will you, as city manager, handle these requests?

Answered: 1 week ago