Question: class Bag: def __init__(self): Initialize an empty bag. >>> bag = Bag() >>> bag [] raise NotImplementedError(__init__ hasn't been implemented.) def __str__(self) -> str:
![Bag() >>> bag [] """ raise NotImplementedError("__init__ hasn't been implemented.") def __str__(self)](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f45e36580d9_93366f45e35f0b7d.jpg)

![Bag() >>> for x in [1, 2, 3, 4]: ... bag.add(x) ...](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f45e37a31ea_93566f45e373b55e.jpg)
![>>> str(bag) '[1, 2, 3, 4]' """ return str(self._items) def __repr__(self) ->](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f45e384a61e_93566f45e37deff6.jpg)
Exercise 1: Read the docstring for _ _init__A Bag object should have one instance variable, names _items, which is initialized to an empty instance of Python's list type. Replace the raise statement with a correct implementation of the method. Use this test to check if your method is correct: >>> bag = Bag() >>> bag._items [] # Shows that _items refers to an empty list. Exercise 2: Read the docstring for __repr__ Replace the raise statement with a correct implementation of the method. Use the shell to test __repr__. (You'll only be able to test if __repr__ works with an empty bag, because we don't yet have a way to put items in a bag.) Exercise 3: Read the docstring for add. Replace the raise statement with a correct implementation of the method. Use the shell to test add. Now that you can put items in a bag, you can also verify that __repr__ works with a bag that contains one or more items. You should also run the example in the docstring for -_iter__, to convince yourself that we can iterate over the items in a bag. Exercise 4: Try this experiment: >>> bag = Bag() >>> len(bag) In order for Python's built-in len function to work with Bag objects, we need to define a __len__ method in the class. Read the docstring for __len__. Replace the raise statement with a correct implementation of the method. Use the shell to test __len Exercise 5: Try this experiment: >>> bag - Bago >>>2 in bag In order for Python's in operator to work with Bag objects, we need to define a__contains__method in the class. Read the docstring for _contains Replace the ralse statement with a correct implementation of the method. Use the shell to test __contains Exercise 6: Read the docstring for count. Replace the raise statement with a correct implementation of the method. Use the shell to test count. Exercise 7: Read the docstring for remove. Replace the raise statement with a correct implementation of the method. When the bag is empty, the method should raise a KeyError exception that displays the message,"bag.remove(x): remove from empty bag" 3 . When the bag has no instances of the item we want to remove, the method should raise a ValueError exception that displays the message, "bag.remove(x): x not in bag. Use the shell to test remove. Exercise 8: Read the docstring for grab Replace the raise statement with a correct implementation of the method. Hint: have a look at the documentation for Python's random module: https://docs.python.org/3/library/random.html. When the bag is empty, the method should raise a KeyError exception that displays the message, 'bag.grabo: grab from empty bag". Use the shell to test grab. # An implementation of ADT Bag that uses Python's list type as the i underlying data structure. class Bag def __init__(self): ***Initialize an empty bag. >>> bag = Bago >>> bag 0 raise NotlmplementedError("__init__hasn't been implemented.") def _ _str__(self) -> str: ***Return a string representation of this bad. >>> bag = Bago >>> for x in (1, 2, 3, 4): bag.add(x) >>> str(bag) '[1, 2, 3, 4)' return str(self._items) def __repr__(self) -> str: ***Return a string representation of this bag. This string is identical to the one returned by __str__ >>> bag = Bago >>> for x in (1, 2, 3, 4]: ... bag.add(x) >>> repr(bag) "[1, 2, 3, 4] >>> bag (1,2,3,4] raise NotimplementedError("__repr__hasn't been implemented.') def __iter__(self): **Return an iterator for this bag. >>> bag = Bago >>> for x in (1,2,3,4]: ***Return an iterator for this bag. >>> bag = Bago >>> for x in (1, 2, 3, 4]: ... bag.add(x) >>> for x in bag: .print(x) 1 2 3 4 return iter(self._items) def add(self, item: int) -> None: ***Add item to this bag. >>> bag - Bago >>> for x in (3, 1, 2, 3, 4); bag.add(x) >>> bag [3, 1,2,3,4 raise NotImplementedError("add hasn't been implemented.") def __len_(self) -> int: ***Return the number of items in this bag. >>> bag = Bago >>> len(bag) 0 >>> for x in (1, 2, 3, 4]: .bag.add(x) >>> len(bag) 4 4 raise NotImplementedError("_len__hasn't been implemented.") def__contains__(self, item: int) -> bool: ***Return True if item is in the bag. >>>bag = Bago >>> 2 in bag False >>> for x in (1, 2, 3, 4): ... bag.add(x) >>> 2 in bag True >>>7in bag False raise NotImplemented Error{"__contains__hasn't been implemented.") def count(self, item:int) -> int: ***Return the total number of occurrences of item in this bag. >>> bag = Bago >>> for x in (3, 1, 2, 3, 4); bag.add(x) 14 >>> bag (3.1. 2,3,4 bag.count(3) 2 raise NotimplementedError("count hasn't been implemented.") def remove(self, item: int) -> int ***Remove and return one instance of item from this bag. Raises KeyError if the bag is empty. Raises ValueError if item is not in the bag. >>>bag = Bago) >>> for x in (3, 1,2,3,4]: Raises KeyError if the bag is empty. Raises ValueError if item is not in the bag. >>> bag = Bago >>> for xin (3, 1, 2, 3, 4); ... bag.add(x) >>> bag.remove(3) 3 >>> bag [1, 2, 3, 4 if the first 3 was removed (3, 1, 2, 4) if the second 3 was removed raise NotlmplementedError("remove hasn't been implemented.") def grab(self) -> int: ***Remove and return a randomly selected item from this bag, Raises KeyError if the bag is empty. >>>bag = Bago >>> for xin (3, 1, 2, 3, 4]: ... bag.add(x) >>>bag.grabo 3 # (or 1 or 2 or 4) >>> bag [1, 2, 3, 4] if the first 3 was removed [3.1, 2, 4] if the second 3 was removed raise NotimplementedError("grab hasn't been implemented.")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
