doing this for a homework, don't understand how this would work, so explaination with steps will help lot. The question is hard understand so have not been able to understand and try. Here is the question,divided into 3 parts. You are given the following superclass. Do not modify this.
class Container(object):
""" Holds hashable objects. Objects may occur 0 or more times """
def __init__(self):
""" Creates a new container with no objects in it. I.e., any object
occurs 0 times in self. """
self.vals = {}
def insert(self, e):
""" assumes e is hashable
Increases the number times e occurs in self by 1. """
try:
self.vals[e] += 1
except:
self.vals[e] = 1
def __str__(self):
s = ""
for i in sorted(self.vals.keys()):
if self.vals[i] != 0:
s += str(i)+":"+str(self.vals[i])+"\n"
return s
Write a class that implements the specifications below. Do not override any methods of Container.
class Bag(Container):
def remove(self, e):
""" assumes e is hashable
If e occurs one or more times in self, reduces the number of
times it occurs in self by 1. Otherwise does nothing. """
# write code here
def count(self, e):
""" assumes e is hashable
Returns the number of times e occurs in self. """
# write code here
• For example, d1 = Bag()
d1.insert(4)
d1.insert(4)
print(d1)
d1.remove(2)
print(d1)
prints 4:2
4:2
• For example, d1 = Bag()
d1.insert(4)
d1.insert(4)
d1.insert(4)
print(d1.count(2))
print(d1.count(4))
prints 0
3
Second part:
Write a method in Bag such that if b1 and b2 were bags then b1+b2 gives a new bag representing the union of the two bags.
• For example, a = Bag()
a.insert(4)
a.insert(3)
b = Bag()
b.insert(4)
print(a+b)
prints 3:1
4:2
third part:
Write a class that implements the specifications below. Do not override any methods of Container.
class ASet(Container):
def remove(self, e):
"""assumes e is hashable
removes e from self"""
# write code here
def is_in(self, e):
"""assumes e is hashable
returns True if e has been inserted in self and
not subsequently removed, and False otherwise."""
# write code here
• For example, d1 = ASet()
d1.insert(4)
d1.insert(4)
d1.remove(2)
print(d1)
d1.remove(4)
print(d1)
prints 4:2 # from d1.remove(2) print
# (empty) from d1.remove(4) print
• For example, d1 = ASet()
d1.insert(4)
print(d1.is_in(4))
d1.insert(5)
print(d1.is_in(5))
d1.remove(5)
print(d1.is_in(5))
prints True
True
False
Thank you.
If you want to write a subclass, the first thing you need to do is understand the class you want to subclass. So, the first thing you need to do is understand what Container
does.
It has two magic methods, __init__
and __str__
, and one ordinary method, insert
. Explore insert
first by doing this:
d1 = Container()
d1.insert(4)
print(d1)
d1.insert(2)
print(d1)
d1.insert(4)
print(d1)
You get this output:
4:1
2:1
4:1
2:1
4:2
There are 3 sets of responses, one from each print()
call. Can you see what is happening? When you insert 4
, you see 4:1
. If you insert 4
a second time, then you see 4:2
. In other words, the string representation you are seeing is value:
count.
This works because Container
has the member vals
which is a dictionary. Each item in the dictionary is value:
count, just like in the string representation.
Your first task is to write a subclass Bag
that does everything that Container
does, but also has the methods remove
and count
.
The method count
just produces the same answer for one value in Container
that __str__
produces for all values. Choose the corresponding value out of the dictionary and return the number of occurrences. Note that it is okay to ask for the count of a value that isn't there: return 0
in that case.
class Bag(Container):
def count(self, e):
return self.vals.get(e,0)
Check that this works:
d1 = Bag()
d1.insert(4)
d1.insert(4)
print(d1.count(4))
The other half of this bit is to write remove
which is the opposite of insert
. What does insert
do? If the value you are inserting is already in vals
, it increases the count, otherwise it sets the count to 1. So remove
needs to decrease the count, and if it goes to zero, removes the item from the dictionary. Note that it is okay to try and remove a value that isn't there: just ignore it in that case.
def remove(self, e):
if e not in self.vals:
return
self.vals[e] -= 1
if self.vals[e] < 1:
del(self.vals[e])
Be careful when adding this bit of code. The indentation needs to line up with count
.
Now that you have the basics, your next task is to write an __add__
method that adds two bags together. In other words given Bag
s a
and b
, you need to combine a.vals
and b.vals
. Start with a copy of a
and then add the contents of b
to it. You already have a method do to the adding: use the method insert
.
def __add__(self, other):
result = self.__class__()
result.vals.update(self.vals)
for value,count in other.vals.items():
for _ in range(count):
result.insert(value)
return result
Be careful when adding this bit of code. The indentation needs to line up with count
.
The third part of your question is really a repeat of the first part. The remove
method is the same. The is_in
method is the same as count
, except that it returns True
or False
instead of a number.