Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class Bag: def __init__(self): Initialize an empty bag. >>> bag = Bag() >>> bag [] raise NotImplementedError(__init__ hasn't been implemented.) def __str__(self) -> str:

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
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.")
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

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 And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 2 Lncs 13427

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124251, 978-3031124259

More Books

Students also viewed these Databases questions

Question

What are the five forces that determine an industrys profitability?

Answered: 1 week ago

Question

What is a verb?

Answered: 1 week ago