Question
Please help with this problem in Python. Below is the starter code, I need help implementing the resize function. I have worked on the append
Please help with this problem in Python. Below is the starter code, I need help implementing the resize function. I have worked on the append method, I need help with methods.
Instructions:
Implement a Bag ADT class by completing the skeleton code provided in the file bag_da.py. You will use the DynamicArray class that you implemented in Part 1 of this assignment as the underlying data storage for your Bag ADT. 2. Once completed, your implementation will include the following methods: add() remove() count() clear() equal() __iter__() __next__() 3. We will test your implementation with different types of objects, not just integers. We guarantee that all such objects will have correct implementation of methods __eq__(), __lt__(), __gt__(), __ge__(), __le__(), and __str__().
RESTRICTIONS: You are NOT allowed to use ANY built-in Python data structures and/or their methods in any of your solutions. This includes built-in Python lists, dictionaries, or anything else. You are also not allowed to directly access any variables of the DynamicArray class (e.g. self._size, self._capacity, and self._data). Access to DynamicArray variables must only be done by using the DynamicArray class methods
#starter code for dynamic array import
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}")
#starter code for Bag
class Bag: def __init__(self, start_bag=None): """ Init new bag based on Dynamic Array DO NOT CHANGE THIS METHOD IN ANY WAY """ self._da = DynamicArray() # populate bag with initial values (if provided) # before using this feature, implement add() method if start_bag is not None: for value in start_bag: self.add(value) def __str__(self) -> str: """ Return content of stack in human-readable form DO NOT CHANGE THIS METHOD IN ANY WAY """ out = "BAG: " + str(self._da.length()) + " elements. [" out += ', '.join([str(self._da.get_at_index(_)) for _ in range(self._da.length())]) return out + ']' def size(self) -> int: """ Return total number of items currently in the bag DO NOT CHANGE THIS METHOD IN ANY WAY """ return self._da.length()
# skeleton code
def add(self, value: object) -> None: """ TODO: Write this implementation """ pass def remove(self, value: object) -> bool: """ TODO: Write this implementation """ pass def count(self, value: object) -> int: """ TODO: Write this implementation """ pass def clear(self) -> None: """ TODO: Write this implementation """ pass def equal(self, second_bag: "Bag") -> bool: """ TODO: Write this implementation """ pass def __iter__(self): """ TODO: Write this implementation """ pass def __next__(self): """ TODO: Write this implementation """ pass
#testing below
if __name__ == "__main__": print(" # add example 1") bag = Bag() print(bag) values = [10, 20, 30, 10, 20, 30] for value in values: bag.add(value) print(bag) print(" # remove example 1") bag = Bag([1, 2, 3, 1, 2, 3, 1, 2, 3]) print(bag) print(bag.remove(7), bag) print(bag.remove(3), bag) print(bag.remove(3), bag) print(bag.remove(3), bag) print(bag.remove(3), bag) print(" # count example 1") bag = Bag([1, 2, 3, 1, 2, 2]) print(bag, bag.count(1), bag.count(2), bag.count(3), bag.count(4)) print(" # clear example 1") bag = Bag([1, 2, 3, 1, 2, 3]) print(bag) bag.clear() print(bag) print(" # equal example 1") bag1 = Bag([10, 20, 30, 40, 50, 60]) bag2 = Bag([60, 50, 40, 30, 20, 10]) bag3 = Bag([10, 20, 30, 40, 50]) bag_empty = Bag() print(bag1, bag2, bag3, bag_empty, sep=" ") print(bag1.equal(bag2), bag2.equal(bag1)) print(bag1.equal(bag3), bag3.equal(bag1)) print(bag2.equal(bag3), bag3.equal(bag2)) print(bag1.equal(bag_empty), bag_empty.equal(bag1)) print(bag_empty.equal(bag_empty)) print(bag1, bag2, bag3, bag_empty, sep=" ") bag1 = Bag([100, 200, 300, 200]) bag2 = Bag([100, 200, 30, 100]) print(bag1.equal(bag2)) print(" # __iter__(), __next__() example 1") bag = Bag([5, 4, -8, 7, 10]) print(bag) for item in bag: print(item) print(" # __iter__(), __next__() example 2") bag = Bag(["orange", "apple", "pizza", "ice cream"]) print(bag) for item in bag: print(item)
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