如果没有直接指定全局变量,那么它是如何递增的?

时间:2018-02-22 08:49:58

标签: python debugging queue circular-buffer

我正在开发一个小型项目,我将队列实现为循环数组。作为一项挑战,我被指派在实现此ADT时不使用任何列表函数,例如append。假设我只需要在队列满时重新调整队列大小。当我通过调试器运行我的代码并逐步执行它时,我发现问题围绕我的self.read值(读指针),这是有问题的全局变量。这对我来说很困惑,因为我受影响的函数都不会增加指针。有人能为我解释这个问题吗?

此类编码为:

class CircularQueue(object):
    def __init__(self, capacity=2):
        """
        Initialize the queue to be empty with a fixed capacity
        :param capacity: Initial size of the queue
        """
        self.capacity = capacity
        self.size = 0
        self.list = [0] * self.capacity
        self.sum = 0
        self.read = 0
        self.write = 0

    def __eq__(self, other):
        return self.capacity == other.capacity 
               and self.size == other.size 
               and self.read == other.read 
               and self.write == other.write   

    def __str__(self):
        if self.size == 0:
            return "Queue is empty"

        content = ""
        while ((self.read + 1) % self.size) != self.write:

            content = content + str(self.list[self.read]) + " -> "
            self.read = (self.read + 1) % self.size
        content = content[:-3]
        return f"Contents: {content}"
    __repr__ = __str__

我感兴趣的部分是我的入队和调整大小的功能:

def resize(self):
    bigger = [None] * (self.capacity * 2) #create bigger queue
    b_ind = 0
    read_ptr = self.read
    while read_ptr != (self.write + 1): #iterate through old queue to copy into new queue
        bigger[b_ind] = self.list[read_ptr]
        b_ind += 1
        read_ptr += 1
    self.capacity *= 2 #setting capacity
    self.list = bigger #setting new list as queue
    self.read = 0 #normalize queue
    self.write = b_ind

def enqueue(self, number):

    if self.size == 0: #if queue was originally empty
        self.list[self.read] = number
        self.write += 1
    else:
        self.list[self.write] = number #add onto end of queue
        if ((self.write + 1) % self.capacity == self.read): #resize if queue loops back and the write pointer is the same as the read pointer
            self.resize()
        else:
            self.write = (self.write + 1) % self.capacity #set write pointer
    self.sum += number #add to sum
    self.size += 1  # increment size

这是我为我的代码运行的测试用例:

queue = CircularQueue()
queue.enqueue(23)
queue.enqueue(42)
queue.enqueue(2)
queue.enqueue(195)

print(queue)

1 个答案:

答案 0 :(得分:1)

在打印时,您会改变队列的状态。 print()调用__str__,该方法会改变您的状态:

self.read = (self.read + 1) % self.size

使用局部变量而不是self.read

def __str__(self):
    if self.size == 0:
        return "Queue is empty"

    content = ""
    read = self.read
    while (read % self.capacity) != self.write:
        if content:
            content += ' -> '
        content += str(self.list[read])
        read = (read + 1) % self.capacity
    return f"Contents: {content}"

注意while循环条件;你想看看当前读取位置是否与write位置不同(意味着你可以显示当前值),而不是下一个位置,并且你想要绕回容量

如果至少已将一个条目添加到content,我会稍微调整您的分隔符处理以仅在值之间添加箭头,这样可以避免再次删除部分。

使用固定__str__方法进行演示(未进行其他更改):

>>> queue = CircularQueue()
>>> print(queue)
Queue is empty
>>> queue.enqueue(23)
>>> print(queue)
Contents: 23
>>> queue.enqueue(42)
>>> print(queue)
Contents: 23 -> 42
>>> queue.enqueue(2)
>>> print(queue)
Contents: 23 -> 42 -> 2
>>> queue.enqueue(195)
>>> print(queue)
Contents: 23 -> 42 -> 2 -> 195