# Question: Problem 6-1 0.0/15.0 points (graded)

\$30.00

Category:

## Description

Question: Problem 6-1 0.0/15.0 points (graded)

1. You are given the following superclass. Do not modify this.
2. class Container(object):
3.   “”” Holds hashable objects. Objects may occur 0 or more times “””
4.   def __init__(self):
5.       “”” Creates a new container with no objects in it. I.e., any object
6.           occurs 0 times in self. “””
7. vals = {}
8.   def insert(self, e):
9.       “”” assumes e is hashable
10.           Increases the number times e occurs in self by 1. “””
11.       try:
12. vals[e] += 1
13.       except:
14. vals[e] = 1
15.   def __str__(self):
16.       s = “”
17.       for i in sorted(self.vals.keys()):
18.           if self.vals[i] != 0:
19.               s += str(i)+”:”+str(self.vals[i])+”\n”
20.       return s
21. Write a class that implements the specifications below. Do not override any methods of Container.
22. class Bag(Container):
23.   def remove(self, e):
24.       “”” assumes e is hashable
25.           If e occurs in self, reduces the number of
26.           times it occurs in self by 1. Otherwise does nothing. “””
27.       # write code here
28.   def count(self, e):
29.       “”” assumes e is hashable
30.           Returns the number of times e occurs in self. “””
31.       # write code here
32.   For example,
33.   d1 = Bag()
34. insert(4)
35. insert(4)
36.   print(d1)
37. remove(2)
38.   print(d1)
39.   prints
40.   4:2
41.   4:2
42.   For example,
43.   d1 = Bag()
44. insert(4)
45. insert(4)
46. insert(4)
47.   print(d1.count(2))
48.   print(d1.count(4))
49.   prints
50.   0

3

error: Content is protected !!