如何实现 process_deque(commands) 来测试我的 Deque 函数?

how to Implement process_deque(commands) to test my Deque function?

首先,我需要实现一个 Deque 函数。

push_front 向双端队列的头部添加一个键

push_back 在双端队列的尾部添加一个键

pop_front 从双端队列的头部提取一个键并return保存它

pop_back 从双端队列的尾部提取一个键并return保存它

front returns head element without removed it

back returns 尾元素而不删除它

size returns 双端队列中元素的个数

clear 删除双端队列中的所有元素

使用此方法和错误处理实现 Deque class。对于 pop_frontpop_front、pop_backpop_back,前、后方法必须检查双端队列中是否有元素。如果它为空,则该方法必须 return 字符串“error”而不是数值。

push_front, push_back, clear方法有到 return 字符串 "ok".

最终实施 process_deque(commands),它接受命令列表和 returns 每个命令的答案列表。

class Deque:

    def __init__(self, max_len = 60000):
        self.max_len = max_len + 1
        self.queue = [0] * self.max_len
        self.head = 0
        self.tail = 0


    def push_front(self, key):
        self.queue[self.head] = key
        self.head= (self.head - 1) % self.max_len
        return "ok"

    def push_back(self, key):
        self.queue[self.tail] = key
        self.tail = (self.tail + 1) % self.max_len
        return "ok"


    def pop_front(self):
        if self.head == self.tail:
            return "error"
        else:
            res = self.queue[self.head]
            self.head = (self.head + 1) % self.max_len
            return res

    def pop_back(self):
        res = self.queue[self.tail]
        self.tail = (self.tail - 1) % self.max_len
        return res

    def front(self):
        if self.head == self.tail:
            return "error"
        else:
            return self.queue[self.head]

    def back(self):
        if self.head == self.tail:
            return "error"
        else:
            return self.queue[self.tail]

    def clear(self):
        self.queue = []
        return "ok"


    def size(self):
        return len(self.queue)


def process_deque(commands):


if __name__ == "__main__": # ***This is the 3 test case***
    test_cmd = ["push_front 1", "push_front 2", "push_back 6", "front", "back", "clear", "size", "back"]
    # should print ["ok", "ok", "ok", 2, 6, "ok", 0, "error"]
    print(process_deque(test_cmd))

    test_cmd = ["pop_front", "back", "push_back 2", "size"]
    # should print ["error", "error", "ok", 1]
    print(process_deque(test_cmd))

    test_cmd = ["push_back 1", "push_front 10", "push_front 4", "push_front 5", "back", "pop_back", "pop_back", "back"]
    # should print ["ok", "ok", "ok", "ok", 1, 1, 10, 4]
    print(process_deque(test_cmd))

我不确定如何实施 process_deque(命令)来测试案例。

谢谢。

我只是调整了我的双端队列函数,下面的代码更干净,因为我不知道如何实现 process_deque(命令),所以我根本没有测试它。

class Deque:

    def __init__(self):
        self.item = []


    def push_front(self, key):
        self.item.append(key)
        return "ok"

    def push_back(self, key):
        self.items.insert(0,key)
        return "ok"


    def pop_front(self):
        if self.head == self.tail:
            return "error"
        else:
            self.items.pop()

    def pop_back(self):
        return self.items.pop(0)

    def front(self):
        if self.head == self.tail:
            return "error"
        else:
            return self.item[0]

    def back(self):
        if self.head == self.tail:
            return "error"
        else:
            return self.item[-1]

    def clear(self):
        self.queue = []
        return "ok"


    def size(self):
        return len(self.queue)

您可以使用 getattr 方法获取上述方法 here

def process_deque(commands):
    d = Deque()
    output = []
    for each_command in commands:
        splitted = each_command.split(" ")
        method_name = splitted[0]
        arg = splitted[1] if len(splitted)==2 else None
        method = getattr(d, method_name)
        if(arg):
            output.append(method(arg))
        else:
            output.append(method())
    return output

但是您的 deque class 方法实现 不正确 。 这不在这个问题的范围内,所以为了证明上述方法是正确的,我使用 python list 本身可以充当 deque.

class Deque:

    def __init__(self, max_len = 60000):
#         self.max_len = max_len + 1
#         self.queue = [0] * self.max_len
#         self.head = 0
#         self.tail = 0
        self.queue = []


    def push_front(self, key):
#         self.queue[self.head] = key
#         self.head= (self.head - 1) % self.max_len
        self.queue.insert(0, key)
        return "ok"

    def push_back(self, key):
#         self.queue[self.tail] = key
#         self.tail = (self.tail + 1) % self.max_len
        self.queue.append(key)
        return "ok"


    def pop_front(self):
#         if self.head == self.tail:
#             return "error"
#         else:
#             res = self.queue[self.head]
#             self.head = (self.head + 1) % self.max_len
#             return res
        if len(self.queue) == 0:
            return "error"
        else:
            res = self.queue.pop(0)
            return res

    def pop_back(self):
#         res = self.queue[self.tail]
#         self.tail = (self.tail - 1) % self.max_len
        if len(self.queue) == 0:
            return "error"
        else:
            res = self.queue.pop()
            return res
        return res

    def front(self):
        if len(self.queue) == 0:
            return "error"
        else:
            return self.queue[0]

    def back(self):
        if len(self.queue) == 0:
            return "error"
        else:
            return self.queue[-1]

    def clear(self):
        self.queue.clear()
        return "ok"


    def size(self):
        return len(self.queue)


def process_deque(commands):
    d = Deque()
    output = []
    for each_command in commands:
        splitted = each_command.split(" ")
        method_name = splitted[0]
        arg = splitted[1] if len(splitted)==2 else None
        method = getattr(d, method_name)
        if(arg):
            output.append(method(arg))
        else:
            output.append(method())
    return output
        


if __name__ == "__main__": # ***This is the 3 test case***
    test_cmd = ["push_front 1", "push_front 2", "push_back 6", "front", "back", "clear", "size", "back"]
    # should print ["ok", "ok", "ok", 2, 6, "ok", 0, "error"]
    print(process_deque(test_cmd))

    test_cmd = ["pop_front", "back", "push_back 2", "size"]
    # should print ["error", "error", "ok", 1]
    print(process_deque(test_cmd))

    test_cmd = ["push_back 1", "push_front 10", "push_front 4", "push_front 5", "back", "pop_back", "pop_back", "back"]
    # should print ["ok", "ok", "ok", "ok", 1, 1, 10, 4]
    print(process_deque(test_cmd))

输出

['ok', 'ok', 'ok', '2', '6', 'ok', 0, 'error']
['error', 'error', 'ok', 1]
['ok', 'ok', 'ok', 'ok', '1', '1', '10', '4']