Queue
Deque in Python
Write deque functions and a palindrome checker.
In Python, a deque can again be a list: myDeque = list().
| Operation | Python |
|---|---|
| insertFront | myDeque.insert(0, element) |
| insertRear | myDeque.append(element) |
| deletionFront | myDeque.pop(0) |
| deletionRear | myDeque.pop() |
| getFront | myDeque[0] |
| getRear | myDeque[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.
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.