Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. """
  2. Implement the class ArrayDeque, including the following operations:
  3. ● push_back
  4. ○ Takes a parameter and adds its value to the back of the deque
  5. ● push_front
  6. ○ Takes a parameter and adds its value to the front of the deque
  7. ● pop_back
  8. ○ Removes the item from the back of the deque and returns its value
  9. ■ If the deque is empty, return None
  10. ● pop_front
  11. ○ Removes the item from the front of the deque and returns its value
  12. ■ If the deque is empty, return None
  13. ● get_size
  14. ○ Returns the number of items currently in the deque
  15. ● __str__
  16. ○ Returns a string with all the items in the deque, separated by a single space
  17. There must not be a limit on how many items can be added to the list. Limitations on the use of
  18. python lists apply to this assignment, as they have in previous assignments on arrays.
  19. For a bonus 5%, implement all operations ( apart from __str__ ) O(1) (amortized)
  20. """
  21. class ArrayDeque():
  22. def __init__(self):
  23. self.size = 10
  24. self.first = 0
  25. self.array = [None] * self.size
  26. self.length = 0
  27.  
  28. def __str__(self):
  29. return str(self.array)
  30.  
  31. def __len__(self):
  32. return self._length
  33.  
  34. def get_size(self):
  35. return len(self.array)
  36.  
  37. def resize(self, new_size, new_value=0):
  38. """Resize to biggest or lesser size."""
  39. element_size = len(self.array[0])
  40. if new_size > len(self.array):
  41. new_size = new_size - 1
  42. while len(self.array)<=new_size:
  43. n = tuple(new_value for i in range(element_size))
  44. array.append(n)
  45. else:
  46. self.array = self.array[:new_size]
  47. return self.array
  48.  
  49. def push_back(self, number):
  50. """add item to end of deque"""
  51. if(self.length == len(self.array)):
  52. self.array.resize(self.length + 1, number)
  53. addix = (self.first + self.length)%(len(self.array))
  54. self.array[addix] = number
  55. self.length += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement