Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#THIS IS THE INCOMPLETE IMPLEMENTATION OF CLASS BAG. #OPENED IN WING 101 # An implementation of ADT Bag that uses Python's list type as the

image text in transcribedimage text in transcribedimage text in transcribed

#THIS IS THE INCOMPLETE IMPLEMENTATION OF CLASS BAG. #OPENED IN WING 101

# 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.")

Type or paste question here

page. Open this file in Wing 101. It contains an incomplete implementation of class Bag. You've been provided with two methods: _str_returns a string representation of the bag. This method is simple: return str(self._items) Instance variable_items refers to the list that stores the bag's contents. The method simply returns the string returned by the underlying list's _str_method. _iter_returns an object that can be used to iterate over the bag. In other words, if shopping_bag refers to an instance of Bag, you can write: for item in shopping_bag: # Print the bag's contents one-by-one. print(item) This method requests the underlying list to create an iterator, and returns that object: return iter (self._items) "Stub" implementations have been provided for the other methods. If you call any of these 2 methods on a Bag object, Python will throw a NotImplementedError exception. 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: htts://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

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

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions

Question

1. What is Ebola ? 2.Heart is a muscle? 3. Artificial lighting?

Answered: 1 week ago