Firstly, I need to implementing a Deque Function.
push_front adds a key to the head of the deque
push_back adds a key to the tail of the deque
pop_front extracts a key from the head of the deque and returns it
pop_back extracts a key from the tail of the deque and returns it
front returns head element without removing it
back returns tail element without removing it
size returns number of the elements in the deque
clear removes all elements from the deque
Implement Deque class with this methods and error handling. For pop_frontpop_front, pop_backpop_back, front, back the method has to check are there elements in the deque. If it is empty the method has to return string "error" instead of a numeric value.
push_front, push_back, clear methods has to return string "ok".
Finally Implement process_deque(commands) which takes commands list and returns a list of answers to each command.
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))
I am not sure how to implementing the process_deque(commands) to test the case.
Thanks.
I just adjust my Deque function, the below code is much more clean, since I don't know how to implementing the process_deque(commands), so i did not test it at all.
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)