Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 6-1 You are given the following superclass. Do not modify this. Write a

ID: 3869614 • Letter: P

Question

Problem 6-1

You are given the following superclass. Do not modify this.

Write a class that implements the specifications below. Do not override any methods of Container.

For example,

prints

For example,

prints

Problem 6-2

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,

prints

Problem 6-3

Write a class that implements the specifications below. Do not override any methods of Container.

For example,

prints

For example,

prints

Explanation / Answer

1)

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])+" "
return s

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. """
if(e in self.vals ):
self.vals[e] -= 1

def count(self, e):
""" assumes e is hashable
Returns the number of times e occurs in self. """
if(e in self.vals):
return self.vals[e]
else:
return 0
d1 = Bag()
d1.insert(4)
d1.insert(4)
print(d1)
d1.remove(2)
print(d1)

d1 = Bag()
d1.insert(4)
d1.insert(4)
d1.insert(4)
print(d1.count(2))
print(d1.count(4))

'''
sample output

4:2

4:2

0
3

'''

2)

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])+" "
return s

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. """
if(e in self.vals ):
self.vals[e] -= 1

def count(self, e):
""" assumes e is hashable
Returns the number of times e occurs in self. """
if(e in self.vals):
return self.vals[e]
else:
return 0
  
def union(self,b):
# Add b bag to A.
for key in b.vals:
if(key in self.vals):
self.vals[key] += b.vals[key]
else:
self.vals[key] = b.vals[key]
a = Bag()
a.insert(4)
a.insert(3)
b = Bag()
b.insert(4)
print(a)
a.union(b)
print(a)

'''
sample output

3:1
4:1

3:1
4:2

'''

3)

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])+" "
return s

class ASet(Container):
def remove(self, e):
"""assumes e is hashable
removes e from self"""
if(e in self.vals):
del self.vals[e]

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."""
if(e in self.vals):
return True
else:
return False
  

d1 = ASet()
d1.insert(4)
d1.insert(4)
d1.remove(2)
print(d1)
d1.remove(4)
print(d1)

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))

'''
sample output

4:2


True
True
False
'''