Question
PLEASE USE THE ALGORITHM you will implement your own dynamic array datatype. It will behave in a similar manner to the list datatype built into
PLEASE USE THE ALGORITHM
you will implement your own dynamic array datatype. It will behave in a similar manner to the list datatype built into Python. Internally, this class should use a standard, fixed-size array (created by a call to stdarray.create1D) as an attribute to store data. When this array fills up, it should be expanded using the following array resizing algorithm
Array Resize -- Inputs: an array of size `n`, called `array` 1) Determine how much larger to make the new array, we will call this `k` 2) Create a new array, `new`, of size `n` + `k` 3) Copy each element from `array` into `new`, maintaining the indices (`array`[0] goes to `new`[0], and so on) 4) Assign `new` to the name `array`, effectively deleting the old small array and replacing it with the new, big one
The API for this datatype is as follows. Remember that you can do whatever you want inside of the class, so long as the only publicly facing stuff are these methods, and these methods do what they're supposed to do. Preface the names of any non-publicly facing attributes or methods with an underscore (_). Note that "d" is used as a stand-in for the name of the dynamic array object for method calls:
For example, if the array were created as follows,
from darray import DArray array = DArray() array.addelem(1) array.addelem('test') array.addelem(True)
then the object returned by str(array) would be,
[1, 'test', True]
Show transcribed image textOperation Notes DArray(data=None) Constructor d[i] -_getitem__dunder Description Creates a new DArray object. If no input is provide, the dynamic array should be initialized as "empty", with no elements in it and a length of O. If an input array, data, is provided, the DArray object should be initialized such that it contains a copy of data. Access and return the value of the element in the dynamic array d at index i. If the provided index is greater than or equal to the length of the array, raise an IndexError. Access the element in the dynamic array d at index i and set it to be the object referred to by e. If the provided index is greater than or equal to the length of the array, raise an IndexError. Extend the accessible length of the dynamic array d by 1, and add the object referred to be e in this new slot. d[i] = e setitem__dunder d.addelem(e) method lend) |__len__ dunder Return the length of the dynamic array d (number of elements contained within it) NOTE: This number should represent the number of elements actually in the array (either due to the constructor, or addelem). It should not return the length of the backing array (the number of available slots). If the backing array has 100 slots, of which only 5 of them have been expanded into, the length of the array is 5, not 100. str(d) str dunder Return the string representation of the dynamic array d. This representation should contain an opening bracket, followed by a comma separated list of the reprs of the array elements, followed by a closing bracket. (*)
Operation Notes DArray(data=None) Constructor d[i] -_getitem__dunder Description Creates a new DArray object. If no input is provide, the dynamic array should be initialized as "empty", with no elements in it and a length of O. If an input array, data, is provided, the DArray object should be initialized such that it contains a copy of data. Access and return the value of the element in the dynamic array d at index i. If the provided index is greater than or equal to the length of the array, raise an IndexError. Access the element in the dynamic array d at index i and set it to be the object referred to by e. If the provided index is greater than or equal to the length of the array, raise an IndexError. Extend the accessible length of the dynamic array d by 1, and add the object referred to be e in this new slot. d[i] = e setitem__dunder d.addelem(e) method lend) |__len__ dunder Return the length of the dynamic array d (number of elements contained within it) NOTE: This number should represent the number of elements actually in the array (either due to the constructor, or addelem). It should not return the length of the backing array (the number of available slots). If the backing array has 100 slots, of which only 5 of them have been expanded into, the length of the array is 5, not 100. str(d) str dunder Return the string representation of the dynamic array d. This representation should contain an opening bracket, followed by a comma separated list of the reprs of the array elements, followed by a closing bracket. (*)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