Last active
December 18, 2018 17:19
-
-
Save AO8/dfd7f8c66d419a23fe026b20f993c6e8 to your computer and use it in GitHub Desktop.
Deque practice with Python 3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exercise completed as part of Erin Allard's Lynda.com course, | |
# 'Python Data Structures: Stacks, Queues, and Deques' | |
class Deque: | |
"""An ADT that resembles both a Stack (LIFO) and a Queue (FIFO). | |
Items in a deque can be added to and removed from both the back | |
and front. | |
Don't reinvent the wheel. More simply, from collections import deque | |
https://docs.python.org/3/library/collections.html#collections.deque | |
""" | |
def __init__(self): | |
self.items = [] | |
def add_front(self, item): | |
"""Takes an item as a parameter and inserts it into the zeroeth index | |
of the list that is representing the Deque. | |
The runtime is linear, or O(n), because every time you insert into the | |
front of a list, all the other items in the list need to shift one | |
position to the right. | |
""" | |
self.items.insert(0, item) | |
def add_rear(self, item): | |
"""Takes in an item as aparameter and appends that item to the end of | |
the list that is representing the Deque. | |
The runtime is 0(1), or constant time, because appending to the end | |
of a list happens in constant time. | |
""" | |
self.items.append(item) | |
def remove_front(self): | |
"""Removes and returns the item in the zeroeth index of the list, which | |
represents the front of the Deque. | |
The runtime is linear, or O(n), because when we remove an item from the | |
zeroeth index, all the other items have to shift one index to the left. | |
""" | |
if self.items: | |
return self.items.pop(0) | |
else: | |
return None | |
def remove_rear(self): | |
"""Removes and returns the last item of the list, which represents the | |
rear of the Deque. | |
The runtime 0(1), or constant time, because all we're doing is indexing | |
to the end of a list. | |
""" | |
if self.items: | |
return self.items.pop() | |
else: | |
return None | |
def peek_front(self): | |
"""Returns the value found at the zeroeth index of the list, which represents | |
the front of the Deque. | |
The runtime is 0(1), or constant time, because all we're doing is indexing | |
into a list. | |
""" | |
if self.items: | |
return self.items[0] | |
else: | |
return None | |
def peek_rear(self): | |
"""Returns the value found at the -1st, or last, index. | |
The runtime is 0(1), or constant time, because all we're doing is indexing | |
into a list.""" | |
if self.items: | |
return self.items[-1] | |
else: | |
return None | |
def size(self): | |
"""Returns the length of the list, which is representing the Deque. | |
The runtime will be 0(1), or constant time, because all we're doing is | |
finding the length of a list and returning that value.""" | |
return len(self.items) | |
def is_empty(self): | |
"""Checks to see if the list representing our Deque is empty. Returns True | |
if it is, or False if it isn't. | |
The runtime is 0(1), or constant time, because all we're doing is comparing | |
two values. | |
""" | |
return self.items == [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment