Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

here i have a class called dynamicarray (uses ctypes) and I am trying to implment the remove_all function (in main) given a list but im

here i have a class called dynamicarray (uses ctypes) and I am trying to implment the remove_all function (in main) given a list but im having trouble doing this, need some help. i am not sure if remove_all should be in the main or in the class either... ***python

here is the class :

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_all(self,value): """Remove all occurances of value""" self._A = list(filter(lambda a: a != value,self._A )) return self._A #x=[2,1,2,3,4,2] obj =DynamicArray() obj._make_array(2) obj._make_array(1) obj._make_array(2) obj._make_array(3) obj._make_array(2) obj._make_array(4)

here is my main:

import dynamicA

def main(): def remove_all(data,value): """Remove all occurances of value""" self._A = list(filter(lambda a: a != value,self._A )) return self._A numbers = [2,1,2,3,4,2] print("list before removing 2",end="" + str(numbers)) remove_all(data,2) print("list after removing 2",end=""+ str(numbers)) main()

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

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions

Question

4 5 9 9 6 - 3 2 7 2 4 / 5 1 9 2 8 acid test ratio

Answered: 1 week ago