Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will implement various methods of the Dynamic array and the Bag ADT with the provided skeleton source code files below. For detailed instructions and

You will implement various methods of the Dynamic array and the Bag ADT with the provided skeleton source code files below. For detailed
instructions and method descriptions, please read this document very carefully: OSU CS261 A2- W24.pdf darr_()
Use the provided tests in the skeleton code to test your code. You are encouraged to write additional unit tests, however, you are not
permitted to share any additional tests with other students.
Skeleton Code Files:
dynamic array_py.(requires static_array.py from Assignment 1)
bag
da.py. darr
Scoring
Dynamic Array (75 points):
resize()-(10 points)
append()-(6 points)
insert_at_index()-(8 points)
remove_at_index()-(8 points)
slice()-(7 points)
merge()-(8 points)
map()-(6 points)
filter()-(6 points)
reduce()-(6 points)
find_mode()-(10 points)
Bag_(25 points):
add()-(4 points)
remove()-(4 points)
count()-(2 points)
clear()-(2 points)
equal()-(9 points)
-0,_next_0[iterator implementation]-(4 points)
What to Turn In (2 files)
The following Python files should be submitted through Gradescope from this assignment:
dynamic_array.py
bag_da.py
Part 1
from static_array import StaticArray
class DynamicArrayException(Exception):
"""
Custom exception class to be used by Dynamic Array
DO NOT CHANGE THIS CLASS IN ANY WAY
"""
pass
class DynamicArray:
def __init__(self, start_array=None):
"""
Initialize new dynamic array
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self._size =0
self._capacity =4
self._data = StaticArray(self._capacity)
# populate dynamic array with initial values (if provided)
# before using this feature, implement append() method
if start_array is not None:
for value in start_array:
self.append(value)
def __str__(self)-> str:
"""
Return content of dynamic array in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
out ="DYN_ARR Size/Cap: "
out += str(self._size)+"/"+ str(self._capacity)+'['
out +=','.join([str(self._data[_]) for _ in range(self._size)])
return out +']'
def __iter__(self):
"""
Create iterator for loop
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self._index =0
return self
def __next__(self):
"""
Obtain next value and advance iterator
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
try:
value = self[self._index]
except DynamicArrayException:
raise StopIteration
self._index +=1
return value
def get_at_index(self, index: int)-> object:
"""
Return value from given index position
Invalid index raises DynamicArrayException
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
if index 0 or index >= self._size:
raise DynamicArrayException
return self._data[index]
def set_at_index(self, index: int, value: object)-> None:
"""
Store value at given index in the array
Invalid index raises DynamicArrayException
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
if index 0 or index >= self._size:
raise DynamicArrayException
self._data[index]= value
def __getitem__(self, index)-> object:
"""
Same functionality as get_at_index() method above,
but called using array[index] syntax
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return self.get_at_index(index)
def __setitem__(self, index, value)-> None:
"""
Same functionality as set_at_index() method above,
but called using array[index] syntax
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self.set_at_index(index, value)
def is_empty(self)-> bool:
"""
Return True is array is empty / False otherwise
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return self._size ==0
def length(self)-> int:
"""
Return number of elements stored in array
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return self._size
def get_capacity(self)-> int:
"""
Return the capacity of the array
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return self._capacity
def print_da_variables(self)-> None:
"""
Print information contained in the dynamic array.
Used for testing purposes.
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
print(f"Length: {self._size}, Capacity: {self._capacity},{self._data}")
# -----------------------------------------------------------------------
def resize(self, new_capacity: int)-> None:
"""
TODO: Write this implementation
"""
pass
def append(self, value: object)-> None:
"""
TODO: Write this implementation
"""
pass
def insert_at_index(self, index: int, value: object)-> None:
"""
TODO: Write this implementation
"""
pass
def remove_at_index(self, index: int)-> None:
"""
TODO: Write this implementation
"""
pass
def slice(self, start_index: int, size: int)-> "DynamicArray":
"""
TODO: Write this implementation
"""
pass
def merge(self, second_da: "DynamicArray")-> None:
"""
TODO: Write this implementation
"""
pass
def m
image text in transcribed

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