早教吧 育儿知识 作业答案 考试题库 百科 知识分享

python3的问题,急啊classSpecialList:"""Alistthatcanholdalimitednumberofitems."""definit(self,size):"""(SpecialList,int)>>>L=SpecialList(10)>>>L.size10>>>L.valuelist[]

题目详情
python3的问题,急啊
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 compare(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
▼优质解答
答案和解析
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
        []
        """
        self.size = size
        self.value_list = []

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]
        """
        self.value_list.append(new_value)

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
        """
        if not self.value_list:
            return None
        else:
            return self.value_list.pop(-1)

def compare(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.
        """
        self_vls = len(self.value_list)
        oth_vls = len(other.value_list)
        return (self_vls > oth_vls) - (self_vls < oth_vls)