泄漏堆栈功能

时间:2018-02-13 11:17:05

标签: python python-3.x

我需要创建一个LeakyStack,其中堆栈大小为5,容量为10.以下应该是最终结果 -

enter image description here

有些说明是:

如果在某一点上,堆栈(即堆栈的内部数组)如下:[4,1,3,2,5,None,None,None,None,None],其中“4”是堆栈的底部和“5”是堆栈的顶部,并在堆栈中推入一个新的数字“8”,堆栈应如下所示:[无,1,3,2,5,8,无,无,无,无,无]。这意味着“4”被忽略(泄露)。

如果在某一点上堆栈如下:[2,3,无,无,无,无,无,8,9,1],其中“8”是堆栈的底部,“3”是在堆栈的顶部,并且在堆栈中推入新的数字“5”,堆栈将变为:[2,3,5,无,无,无,无,无,9,1]。这意味着“8”被忽略(泄露)。

应考虑其他特殊情况,例如在堆栈为空时执行弹出操作。

这是我的代码。现在我需要在__init__处修复一些问题,最后我认为。

class Full(Exception):
pass

class Empty(Exception):
pass

class MyLeakyStack:

def __init__(self, maxlen, capacity, len):
    # note: this function is partially completed
    self.data = []
    self._maxlen = maxlen
    self._capacity = capacity  # size of the circular array
    self._storage = [None] * capacity  # initialise storage room, this is treated as a circular array

def __len__(self):
    return self.len

def push(self,x):
    if self.len == maxsize:
        del self.data[0]
        self.data.append(x)
    else:
        self.data.append(x)
        self.len += 1

def pop(self):
    self.len = -1
    self.data.pop()

def is_empty(selfs):
   return len(self.data)==0

def __str__(self):
    return ' '.join(str(self.data[i]) for i in range(len(self.data)))

if __name__ == '__main__':

 S = MyLeakyStack(5, 10)   # stack size should be 5 and the capacity of the array should be 10

 for i in range(12):
    try:
        S.push(i)
        print("after push "+str(i), S._storage)
    except Exception as e:
        print(e)

 for i in range(6):
    try:
        a=S.pop()
        print("after pop "+str(a), S._storage)
    except Exception as e:
        print(e, S._storage)

 for i in range(5):
    try:
        S.push(i+100)
        print("after push " + str(i+100), S._storage)
    except Exception as e:
        print(e, S._storage)


a = MyLeakyStack()
a.push(1)
a.push(2)
a.push(3)
a.push(4)
a.push(5)
a.push(6)
a.push(8)
a.push(0)
a.pop(1)
print(len(a))

1 个答案:

答案 0 :(得分:0)

首先,我不明白您为什么将“ len ”传递给 __ init __ 函数,因为您没有使用该参数。

第二,即使您正在使用它(我可以看到您在其他对象函数中调用 self.len ),也不能使用单词“ len”来命名a变量,因为它保留给 len()函数。

因此,我想说的是,在初始化对象时不需要,因为一旦创建对象,它的长度就总是为0。

我更改了您的代码以匹配所需的输出。

class Full(Exception):
    pass

class Empty(Exception):
    pass

class MyLeakyStack:

    def __init__(self, maxlen, capacity):
        # note: this function is partially completed
        self._maxlen = maxlen
        self._capacity = capacity  # size of the circular array
        self._storage = [None] * capacity  # initialise storage room, this is treated as a circular array
        self.f = 0
        self.r = 0

    def __len__(self):
        return (self._capacity - self.r + self.f) % self._capacity

    def push(self,x):
        self._storage[self.f] = x
        self.f = (self.f + 1) % self._capacity
        if len(self) > self._maxlen:
            l = self._storage[self.r]
            self._storage[self.r] = None
            self.r = (self.r + 1) % self._capacity
            raise Full("Reach stack limit, forget element " + str(l))

    def pop(self):
        if self.is_empty():
            raise Empty("Stack is empty")

        pop = self._storage[self.f - 1]
        self._storage[self.f - 1] = None
        self.f = (self.f - 1) % self._capacity
        return pop

    def is_empty(self):
        return self.f == self.r

    def __str__(self):
        return " " + str(self._storage)

if __name__ == '__main__':

    S = MyLeakyStack(5, 10)   # stack size should be 5 and the capacity of the array should be 10

    for i in range(12):
        try:
            S.push(i)
            print("after push "+str(i), S._storage)
        except Exception as e:
            print(e)
            print("after push "+str(i), S._storage)

    for i in range(6):
        try:
            a=S.pop()
            print("after pop "+str(a), S._storage)
        except Exception as e:
            print(e, S._storage)

    for i in range(5):
        try:
            S.push(i+100)
            print("after push " + str(i+100), S._storage)
        except Exception as e:
            print(e, S._storage)


a = MyLeakyStack(5, 10)
a.push(1)
a.push(2)
a.push(3)
a.push(4)
a.push(5)
a.push(6)
a.push(8)
a.push(0)
a.pop()
print(str(len(a)))

这是我得到的输出,与要求您提供的输出相同。

after push 0 [0, None, None, None, None, None, None, None, None, None]
after push 1 [0, 1, None, None, None, None, None, None, None, None]
after push 2 [0, 1, 2, None, None, None, None, None, None, None]
after push 3 [0, 1, 2, 3, None, None, None, None, None, None]
after push 4 [0, 1, 2, 3, 4, None, None, None, None, None]
Reach stack limit, forget element 0
after push 5 [None, 1, 2, 3, 4, 5, None, None, None, None]
Reach stack limit, forget element 1
after push 6 [None, None, 2, 3, 4, 5, 6, None, None, None]
Reach stack limit, forget element 2
after push 7 [None, None, None, 3, 4, 5, 6, 7, None, None]
Reach stack limit, forget element 3
after push 8 [None, None, None, None, 4, 5, 6, 7, 8, None]
Reach stack limit, forget element 4
after push 9 [None, None, None, None, None, 5, 6, 7, 8, 9]
Reach stack limit, forget element 5
after push 10 [10, None, None, None, None, None, 6, 7, 8, 9]
Reach stack limit, forget element 6
after push 11 [10, 11, None, None, None, None, None, 7, 8, 9]
after pop 11 [10, None, None, None, None, None, None, 7, 8, 9]
after pop 10 [None, None, None, None, None, None, None, 7, 8, 9]
after pop 9 [None, None, None, None, None, None, None, 7, 8, None]
after pop 8 [None, None, None, None, None, None, None, 7, None, None]
after pop 7 [None, None, None, None, None, None, None, None, None, None]
Stack is empty [None, None, None, None, None, None, None, None, None, None]
after push 100 [None, None, None, None, None, None, None, 100, None, None]
after push 101 [None, None, None, None, None, None, None, 100, 101, None]
after push 102 [None, None, None, None, None, None, None, 100, 101, 102]
after push 103 [103, None, None, None, None, None, None, 100, 101, 102]
after push 104 [103, 104, None, None, None, None, None, 100, 101, 102]
Traceback (most recent call last):
  File "so.py", line 77, in <module>
    a.push(6)
  File "so.py", line 27, in push
    raise Full("Reach stack limit, forget element " + str(l))
__main__.Full: Reach stack limit, forget element 1

考虑到它以错误结尾,因为这是您引发的异常。如果不希望脚本停止运行,则需要使用try-except块。