Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Implement the class ArrayDeque, including the following operations:
- ● push_back
- ○ Takes a parameter and adds its value to the back of the deque
- ● push_front
- ○ Takes a parameter and adds its value to the front of the deque
- ● pop_back
- ○ Removes the item from the back of the deque and returns its value
- ■ If the deque is empty, return None
- ● pop_front
- ○ Removes the item from the front of the deque and returns its value
- ■ If the deque is empty, return None
- ● get_size
- ○ Returns the number of items currently in the deque
- ● __str__
- ○ Returns a string with all the items in the deque, separated by a single space
- There must not be a limit on how many items can be added to the list. Limitations on the use of
- python lists apply to this assignment, as they have in previous assignments on arrays.
- For a bonus 5%, implement all operations ( apart from __str__ ) O(1) (amortized)
- """
- class ArrayDeque():
- def __init__(self):
- self.size = 10
- self.first = 0
- self.array = [None] * self.size
- self.length = 0
- def __str__(self):
- return str(self.array)
- def __len__(self):
- return self._length
- def get_size(self):
- return len(self.array)
- def resize(self, new_size, new_value=0):
- """Resize to biggest or lesser size."""
- element_size = len(self.array[0])
- if new_size > len(self.array):
- new_size = new_size - 1
- while len(self.array)<=new_size:
- n = tuple(new_value for i in range(element_size))
- array.append(n)
- else:
- self.array = self.array[:new_size]
- return self.array
- def push_back(self, number):
- """add item to end of deque"""
- if(self.length == len(self.array)):
- self.array.resize(self.length + 1, number)
- addix = (self.first + self.length)%(len(self.array))
- self.array[addix] = number
- self.length += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement