LearnWave
Learn it. Master it.

Queue

Deque in Python

Write deque functions and a palindrome checker.

In Python, a deque can again be a list: myDeque = list().

OperationPython
insertFrontmyDeque.insert(0, element)
insertRearmyDeque.append(element)
deletionFrontmyDeque.pop(0)
deletionRearmyDeque.pop()
getFrontmyDeque[0]
getRearmyDeque[len(myDeque)-1]

Each function should first check that the deque is not empty before deleting or reading from it. Because the deque is a separate list, every function that tests for emptiness must be given the deque as an argument: isEmpty(myDeque).

For example, if the deque holds z x c v b, then deletionFront() removes z, and deletionRear() removes b, leaving x c v.

The palindrome checker puts these together: append each character of the word, then, while more than one character remains, compare pop(0) with pop(); if any pair differs, the word is not a palindrome.

insert(0, x) at the front; append(x) at the rear.

Practise this lesson

Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.

No account, no email address. Opens the real product.