为家庭作业做这个,不明白这是如何工作的,所以对步骤的解释会有所帮助。 这个问题很难理解所以无法理解和尝试。 这是一个问题,分为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])+"\n"
return s
编写一个实现以下规范的类。不要覆盖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
•例如,d1 = Bag()
d1.insert(4)
d1.insert(4)
print(d1)
d1.remove(2)
print(d1)
prints 4:2
4:2
•例如,d1 = Bag()
d1.insert(4)
d1.insert(4)
d1.insert(4)
print(d1.count(2))
print(d1.count(4))
prints 0
3
第二部分:
在Bag中写一个方法,如果b1和b2是袋子,那么b1 + b2给出一个代表两个袋子结合的新袋子。
•例如,a = Bag()
a.insert(4)
a.insert(3)
b = Bag()
b.insert(4)
print(a+b)
prints 3:1
4:2
第三部分:
编写一个实现以下规范的类。不要覆盖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
•例如,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
谢谢。
答案 0 :(得分:1)
如果你想编写一个子类,你需要做的第一件事是理解你想要子类的类。因此,您需要做的第一件事就是了解Container
的作用。
它有两种神奇的方法,__init__
和__str__
,以及一种普通的方法,insert
。首先通过以下方式探索insert
:
d1 = Container()
d1.insert(4)
print(d1)
d1.insert(2)
print(d1)
d1.insert(4)
print(d1)
你得到这个输出:
4:1
2:1
4:1
2:1
4:2
有3组回复,每组来自print()
次。你能看出发生了什么吗?当您插入4
时,您会看到4:1
。如果您再次插入4
,则会看到4:2
。换句话说,您看到的字符串表示是值 :
count 。
这是有效的,因为Container
的成员vals
是一个字典。字典中的每个项目都是 value :
count ,就像字符串表示一样。
您的第一个任务是编写一个子类Bag
来完成Container
所做的所有事情,但也有方法remove
和count
。
方法count
只为Container
中__str__
为所有值生成的一个值生成相同的答案。从字典中选择相应的值并返回出现次数。请注意,可以询问不存在的值的计数:在这种情况下返回0
。
class Bag(Container):
def count(self, e):
return self.vals.get(e,0)
检查这是否有效:
d1 = Bag()
d1.insert(4)
d1.insert(4)
print(d1.count(4))
这一位的另一半是写remove
,它与insert
相反。 insert
做了什么?如果您要插入的值已经在vals
中,则会增加计数,否则会将计数设置为1.因此remove
需要减少计数,如果它变为零,则删除项目来自字典。请注意,可以尝试删除不存在的值:在这种情况下只需忽略它。
def remove(self, e):
if e not in self.vals:
return
self.vals[e] -= 1
if self.vals[e] < 1:
del(self.vals[e])
添加这段代码时要小心。缩进需要与count
对齐。
现在您已掌握了基础知识,接下来的任务是编写一个__add__
方法,将两个包放在一起。换句话说,Bag
a
和b
,您需要合并a.vals
和b.vals
。从a
的副本开始,然后将b
的内容添加到其中。您已经有一个添加方法:使用方法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
添加这段代码时要小心。缩进需要与count
对齐。
你问题的第三部分实际上是第一部分的重复。 remove
方法是相同的。 is_in
方法与count
相同,只是它返回True
或False
而不是数字。