Question: 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
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 = Bag() >>> 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 raise 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.grab(): grab from empty bag". Use the shell to test grab.
# An implementation of ADT Bag that uses Python's list type as the # underlying data structure.
class Bag:
def __init__(self): """Initialize an empty bag.
>>> bag = Bag() >>> bag [] """ raise NotImplementedError("__init__ hasn't been implemented.")
def __str__(self) -> str: """Return a string representation of this bag.
>>> bag = Bag() >>> 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 = Bag() >>> 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 = Bag() >>> 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 = Bag() >>> 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 = Bag() >>> len(bag) 0 >>> for x in [1, 2, 3, 4]: ... bag.add(x) ... >>> len(bag) 4 """ raise NotImplementedError("__len__ hasn't been implemented.")
def __contains__(self, item: int) -> bool: """Return True if item is in the bag.
>>> bag = Bag() >>> 2 in bag False >>> for x in [1, 2, 3, 4]: ... bag.add(x) ... >>> 2 in bag True >>> 7 in bag False """ raise NotImplementedError("__contains__ hasn't been implemented.")
def count(self, item: int) -> int: """Return the total number of occurrences of item in this bag.
>>> bag = Bag() >>> for x in [3, 1, 2, 3, 4]: ... bag.add(x) ... >>> 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 = Bag() >>> for x in [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 NotImplementedError("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 = Bag() >>> for x in [3, 1, 2, 3, 4]: ... bag.add(x) ... >>> bag.grab() 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
