我是python的新手,我正在打字写一个类"" specialList""。 SpecialList是一个具有两个实例变量的类,value_list和size,value_list引用一个列表,size存储一个整数,表示可以存储在列表中的最大项目数。
这是我到目前为止所做的:
class SpecialList:
"""A list that can hold a limited number of items."""
def __init__(self, size):
""" (SpecialList, int)
>>> L = SpecialList(10)
>>> L.size
10
>>> L.value_list
[]
"""
# complete this code
def push_value(self, new_value):
""" (SpecialList, object) -> NoneType
Append new_value to this list, if there is enough space in the list according to its maximum size.
If there is insufficient space, new_value should not be added to the list.
>>> L = SpecialList(10)
>>> L.push_value(3)
>>> L.value_list
[3]
"""
# complete this code
def pop_most_recent_value(self):
""" (SpecialList) -> object
Precondition: len(self.value_list) != 0
Return the value added most recently to value_list and remove it from the list.
>>> L = SpecialList(10)
>>> L.push_value(3)
>>> L.push_value(4)
>>> L.value_list
[3, 4]
>>> L.pop_most_recent_value()
4
"""
# complete this code
def compar(self, other):
""" (SpecialList, SpecialList) -> int
Return 0 if both SpecialList objects have lists of the same size.
Return 1 if self's list contains more items than other's list.
Return -1 if self's list contains fewer items than other's list.
"""
# complete this code
self.other = []
if range(len(self.value_list)) == range(len(self.other)):
return 0
elif range(len(self.value_list)) > len(self.other):
return 1
else:
return -1
一切正常,但最后一部分,def比较,我没有得到正确的分数.. 请帮忙
答案 0 :(得分:0)
对compare
方法进行一些修改:
self.other = []
other
是SpecialList
的一个实例。所以你需要
比较self
' s value_list
和other
s' value_list
。试试这个:
if len(self.value_list) == len(other.value_list):
return 0
elif len(self.value_list) > len(other.value_list):
return 1
else:
return -1