dsa.deque
Module containing deque classes.
1""" Module containing deque classes. """ 2 3class Deque: 4 """ 5 A static deque implementation that supports appending and popping elements 6 from both ends, with a fixed capacity. 7 """ 8 def __init__(self, capacity: int=10): 9 """ 10 Initialize a deque with a fixed capacity. 11 12 Args: 13 capacity (int): The initial size of the deque (default is 10). 14 """ 15 self._array = [None] * capacity 16 self._left = -1 17 self._right = 0 18 self.count = 0 19 20 def push_front(self, element): 21 """ 22 Push an element at the front of the deque. (synonym for append_left) 23 Raises an exception when the deque is full. 24 25 Args: 26 element: The element to append. 27 28 Raises: 29 Exception: If the deque is full. 30 """ 31 self.append_left(element) 32 33 def push_back(self, element): 34 """ 35 Push an element at the back of the deque. (synonym for append_right) 36 Raises an exception when the deque is full. 37 38 Args: 39 element: The element to append. 40 41 Raises: 42 Exception: If the deque is full. 43 """ 44 self.append_right(element) 45 46 def pop_front(self): 47 """ 48 Pop an element from the front of the deque. (synonym for pop_left) 49 Raises an exception if the deque is empty. 50 51 Returns: 52 The leftmost element of the deque. 53 54 Raises: 55 Exception: If the deque is empty. 56 """ 57 return self.pop_left() 58 59 def pop_back(self): 60 """ 61 Pop an element from the back of the deque. (synonym for pop_right) 62 Raises an exception if the deque is empty. 63 64 Returns: 65 The rightmost element of the deque. 66 67 Raises: 68 Exception: If the deque is empty. 69 """ 70 return self.pop_right() 71 72 def front(self): 73 """ 74 Get the element at the front of the deque without removing it. 75 Raises an exception if the deque is empty. 76 77 Returns: 78 The leftmost element. 79 80 Raises: 81 Exception: If the deque is empty. 82 """ 83 return self.peek_left() 84 85 def back(self): 86 """ 87 Get the element at the back of the deque without removing it. 88 Raises an exception if the deque is empty. 89 90 Returns: 91 The rightmost element. 92 93 Raises: 94 Exception: If the deque is empty. 95 """ 96 return self.peek_right() 97 98 def append_left(self, element): 99 """ 100 Append an element to the left of the deque. Raises an exception when the deque is full. 101 102 Args: 103 element: The element to append. 104 105 Raises: 106 Exception: If the deque is full. 107 """ 108 if self.count >= self.capacity(): 109 raise Exception("Deque Full") 110 if self._left < 0: 111 self._left = self.capacity() - 1 112 self._array[self._left] = element 113 self._left -= 1 114 self.count += 1 115 116 def append_right(self, element): 117 """ 118 Append an element to the right of the deque. Raises an exception when the deque is full. 119 120 Args: 121 element: The element to append. 122 123 Raises: 124 Exception: If the deque is full. 125 """ 126 if self.count >= self.capacity(): 127 raise Exception("Deque Full") 128 if self._right > self.capacity() - 1: 129 self._right = 0 130 self._array[self._right] = element 131 self._right += 1 132 self.count += 1 133 134 def pop_left(self): 135 """ 136 Remove and return the element from the left of the deque. Raises an exception if the deque is empty. 137 138 Returns: 139 The leftmost element of the deque. 140 141 Raises: 142 Exception: If the deque is empty. 143 """ 144 if self.is_empty(): 145 raise Exception("Empty Deque") 146 147 element = self._array[self._left + 1] 148 self._left += 1 149 if self._left + 1 >= len(self._array): 150 self._left = -1 151 self.count -= 1 152 return element 153 154 def pop_right(self): 155 """ 156 Pop an element from right the deque. Raise an exception if the deque is empty. 157 158 Returns: 159 The rightmost element of the deque. 160 161 Raises: 162 Exception: If the deque is empty. 163 """ 164 if self.is_empty(): 165 raise Exception("Empty Deque") 166 element = self._array[self._right - 1] 167 self.count -= 1 168 169 self._right -= 1 170 if self._right < 0: 171 self._right = len(self._array) - 1 172 return element 173 174 def peek_left(self): 175 """ 176 Get the element at the left of the deque without removing it. 177 Raises an exception if the deque is empty. 178 179 Returns: 180 The leftmost element. 181 182 Raises: 183 Exception: If the deque is empty. 184 """ 185 if self.is_empty(): 186 raise Exception("Empty deque") 187 return self._array[self._left + 1] 188 189 def peek_right(self): 190 """ 191 Get the element at the right of the deque without removing it. 192 Raises an exception if the deque is empty. 193 194 Returns: 195 The rightmost element. 196 197 Raises: 198 Exception: If the deque is empty. 199 """ 200 if self.is_empty(): 201 raise Exception("Empty deque") 202 return self._array[self._right - 1] 203 204 def is_empty(self) -> bool: 205 """ 206 Return a Boolean on whether the deque is empty or not. 207 """ 208 return self.count == 0 209 210 @classmethod 211 def from_list(cls, alist: list): 212 """ 213 Create a deque from a given list. Raises an exception if list exceeds the deque's capacity. 214 215 Args: 216 alist: The list to initialize the deque with. 217 218 Returns: 219 a deque instance with elements from the list. 220 221 Raises: 222 Exception: If list exceeds the deque's capacity. 223 """ 224 dq = cls() 225 for e in alist: 226 dq.append_right(e) 227 return dq 228 229 def to_list(self): 230 """ 231 Convert the deque's contents into a list. 232 233 Returns: 234 A list containg the elements in the deque. 235 """ 236 arr = [] 237 for i in range(self.count): 238 index = (i + self._left + 1) % len(self._array) 239 arr.append(self._array[index]) 240 return arr 241 242 def capacity(self) -> int: 243 """ 244 Get the maximum capacity of the deque. 245 246 Returns: 247 The capacity of the deque. 248 """ 249 return len(self._array) 250 251 def __len__(self) -> int: 252 """ 253 Get the number of elements in the deque. 254 255 Returns: 256 The count of elements. 257 """ 258 return self.count 259 260 def __repr__(self): 261 """ 262 String representation of the deque for debugging purposes. 263 264 Returns: 265 A string displaying the contents and size of the deque. 266 """ 267 arr = [] 268 for i in range(self.count): 269 index = (i + self._left + 1) % len(self._array) 270 arr.append(str(self._array[index])) 271 arrstr = ", ".join(arr) 272 return f"[{arrstr}] Count: {self.count} Capacity: {len(self._array)}" # {self.array}" 273 274class DynamicDeque(Deque): 275 """ 276 A dynamic deque implementation that adjusts its capacity as needed. 277 """ 278 def grow(self): 279 """ 280 Helper method to double the capacity of the deque. 281 """ 282 new_array = [ None ] * self.capacity() * 2 283 284 # copy elements 285 for i in range(self.count): 286 index = (i + self._left + 1) % len(self._array) 287 new_array[i] = self._array[index] 288 289 self._array = new_array 290 self._left = -1 291 self._right = self.count 292 293 def shrink(self): 294 """ 295 Helper method to halve the capacity of the deque. 296 """ 297 if self.capacity() < 10: 298 return 299 300 new_capacity = self.capacity() // 2 301 new_array = [ None ] * new_capacity 302 303 # copy elements 304 for i in range(self.count): 305 index = (i + self._left + 1) % len(self._array) 306 new_array[i] = self._array[index] 307 308 self._array = new_array 309 self._left = -1 310 self._right = self.count 311 312 313 def check_capacity(self): 314 """ 315 Helper method to adjust the capacity of the deque based on its count: 316 if count >= capacity, grow the deque 317 if count <= 1/4 of capacity, shrink the deque 318 """ 319 if self.count >= self.capacity(): 320 self.grow() 321 elif self.count * 4 <= self.capacity(): 322 self.shrink() 323 324 def append_left(self, element): 325 """ 326 Append an element to the left, adjusting capacity if needed. 327 328 Args: 329 element: The element to append. 330 """ 331 self.check_capacity() 332 super().append_left(element) 333 334 def append_right(self, element): 335 """ 336 Append an element to the right, adjusting capacity if needed. 337 338 Args: 339 element: The element to append. 340 """ 341 self.check_capacity() 342 super().append_right(element) 343 344 def pop_left(self): 345 """ 346 Remove and return the element from the left, adjusting capacity if needed. 347 348 Returns: 349 The leftmost element. 350 """ 351 self.check_capacity() 352 return super().pop_left() 353 354 355 def pop_right(self): 356 """ 357 Remove and return the element from the right, adjusting capacity if needed. 358 359 Returns: 360 The rightmost element. 361 """ 362 self.check_capacity() 363 return super().pop_right()
4class Deque: 5 """ 6 A static deque implementation that supports appending and popping elements 7 from both ends, with a fixed capacity. 8 """ 9 def __init__(self, capacity: int=10): 10 """ 11 Initialize a deque with a fixed capacity. 12 13 Args: 14 capacity (int): The initial size of the deque (default is 10). 15 """ 16 self._array = [None] * capacity 17 self._left = -1 18 self._right = 0 19 self.count = 0 20 21 def push_front(self, element): 22 """ 23 Push an element at the front of the deque. (synonym for append_left) 24 Raises an exception when the deque is full. 25 26 Args: 27 element: The element to append. 28 29 Raises: 30 Exception: If the deque is full. 31 """ 32 self.append_left(element) 33 34 def push_back(self, element): 35 """ 36 Push an element at the back of the deque. (synonym for append_right) 37 Raises an exception when the deque is full. 38 39 Args: 40 element: The element to append. 41 42 Raises: 43 Exception: If the deque is full. 44 """ 45 self.append_right(element) 46 47 def pop_front(self): 48 """ 49 Pop an element from the front of the deque. (synonym for pop_left) 50 Raises an exception if the deque is empty. 51 52 Returns: 53 The leftmost element of the deque. 54 55 Raises: 56 Exception: If the deque is empty. 57 """ 58 return self.pop_left() 59 60 def pop_back(self): 61 """ 62 Pop an element from the back of the deque. (synonym for pop_right) 63 Raises an exception if the deque is empty. 64 65 Returns: 66 The rightmost element of the deque. 67 68 Raises: 69 Exception: If the deque is empty. 70 """ 71 return self.pop_right() 72 73 def front(self): 74 """ 75 Get the element at the front of the deque without removing it. 76 Raises an exception if the deque is empty. 77 78 Returns: 79 The leftmost element. 80 81 Raises: 82 Exception: If the deque is empty. 83 """ 84 return self.peek_left() 85 86 def back(self): 87 """ 88 Get the element at the back of the deque without removing it. 89 Raises an exception if the deque is empty. 90 91 Returns: 92 The rightmost element. 93 94 Raises: 95 Exception: If the deque is empty. 96 """ 97 return self.peek_right() 98 99 def append_left(self, element): 100 """ 101 Append an element to the left of the deque. Raises an exception when the deque is full. 102 103 Args: 104 element: The element to append. 105 106 Raises: 107 Exception: If the deque is full. 108 """ 109 if self.count >= self.capacity(): 110 raise Exception("Deque Full") 111 if self._left < 0: 112 self._left = self.capacity() - 1 113 self._array[self._left] = element 114 self._left -= 1 115 self.count += 1 116 117 def append_right(self, element): 118 """ 119 Append an element to the right of the deque. Raises an exception when the deque is full. 120 121 Args: 122 element: The element to append. 123 124 Raises: 125 Exception: If the deque is full. 126 """ 127 if self.count >= self.capacity(): 128 raise Exception("Deque Full") 129 if self._right > self.capacity() - 1: 130 self._right = 0 131 self._array[self._right] = element 132 self._right += 1 133 self.count += 1 134 135 def pop_left(self): 136 """ 137 Remove and return the element from the left of the deque. Raises an exception if the deque is empty. 138 139 Returns: 140 The leftmost element of the deque. 141 142 Raises: 143 Exception: If the deque is empty. 144 """ 145 if self.is_empty(): 146 raise Exception("Empty Deque") 147 148 element = self._array[self._left + 1] 149 self._left += 1 150 if self._left + 1 >= len(self._array): 151 self._left = -1 152 self.count -= 1 153 return element 154 155 def pop_right(self): 156 """ 157 Pop an element from right the deque. Raise an exception if the deque is empty. 158 159 Returns: 160 The rightmost element of the deque. 161 162 Raises: 163 Exception: If the deque is empty. 164 """ 165 if self.is_empty(): 166 raise Exception("Empty Deque") 167 element = self._array[self._right - 1] 168 self.count -= 1 169 170 self._right -= 1 171 if self._right < 0: 172 self._right = len(self._array) - 1 173 return element 174 175 def peek_left(self): 176 """ 177 Get the element at the left of the deque without removing it. 178 Raises an exception if the deque is empty. 179 180 Returns: 181 The leftmost element. 182 183 Raises: 184 Exception: If the deque is empty. 185 """ 186 if self.is_empty(): 187 raise Exception("Empty deque") 188 return self._array[self._left + 1] 189 190 def peek_right(self): 191 """ 192 Get the element at the right of the deque without removing it. 193 Raises an exception if the deque is empty. 194 195 Returns: 196 The rightmost element. 197 198 Raises: 199 Exception: If the deque is empty. 200 """ 201 if self.is_empty(): 202 raise Exception("Empty deque") 203 return self._array[self._right - 1] 204 205 def is_empty(self) -> bool: 206 """ 207 Return a Boolean on whether the deque is empty or not. 208 """ 209 return self.count == 0 210 211 @classmethod 212 def from_list(cls, alist: list): 213 """ 214 Create a deque from a given list. Raises an exception if list exceeds the deque's capacity. 215 216 Args: 217 alist: The list to initialize the deque with. 218 219 Returns: 220 a deque instance with elements from the list. 221 222 Raises: 223 Exception: If list exceeds the deque's capacity. 224 """ 225 dq = cls() 226 for e in alist: 227 dq.append_right(e) 228 return dq 229 230 def to_list(self): 231 """ 232 Convert the deque's contents into a list. 233 234 Returns: 235 A list containg the elements in the deque. 236 """ 237 arr = [] 238 for i in range(self.count): 239 index = (i + self._left + 1) % len(self._array) 240 arr.append(self._array[index]) 241 return arr 242 243 def capacity(self) -> int: 244 """ 245 Get the maximum capacity of the deque. 246 247 Returns: 248 The capacity of the deque. 249 """ 250 return len(self._array) 251 252 def __len__(self) -> int: 253 """ 254 Get the number of elements in the deque. 255 256 Returns: 257 The count of elements. 258 """ 259 return self.count 260 261 def __repr__(self): 262 """ 263 String representation of the deque for debugging purposes. 264 265 Returns: 266 A string displaying the contents and size of the deque. 267 """ 268 arr = [] 269 for i in range(self.count): 270 index = (i + self._left + 1) % len(self._array) 271 arr.append(str(self._array[index])) 272 arrstr = ", ".join(arr) 273 return f"[{arrstr}] Count: {self.count} Capacity: {len(self._array)}" # {self.array}"
A static deque implementation that supports appending and popping elements from both ends, with a fixed capacity.
9 def __init__(self, capacity: int=10): 10 """ 11 Initialize a deque with a fixed capacity. 12 13 Args: 14 capacity (int): The initial size of the deque (default is 10). 15 """ 16 self._array = [None] * capacity 17 self._left = -1 18 self._right = 0 19 self.count = 0
Initialize a deque with a fixed capacity.
Args: capacity (int): The initial size of the deque (default is 10).
21 def push_front(self, element): 22 """ 23 Push an element at the front of the deque. (synonym for append_left) 24 Raises an exception when the deque is full. 25 26 Args: 27 element: The element to append. 28 29 Raises: 30 Exception: If the deque is full. 31 """ 32 self.append_left(element)
Push an element at the front of the deque. (synonym for append_left) Raises an exception when the deque is full.
Args: element: The element to append.
Raises: Exception: If the deque is full.
34 def push_back(self, element): 35 """ 36 Push an element at the back of the deque. (synonym for append_right) 37 Raises an exception when the deque is full. 38 39 Args: 40 element: The element to append. 41 42 Raises: 43 Exception: If the deque is full. 44 """ 45 self.append_right(element)
Push an element at the back of the deque. (synonym for append_right) Raises an exception when the deque is full.
Args: element: The element to append.
Raises: Exception: If the deque is full.
47 def pop_front(self): 48 """ 49 Pop an element from the front of the deque. (synonym for pop_left) 50 Raises an exception if the deque is empty. 51 52 Returns: 53 The leftmost element of the deque. 54 55 Raises: 56 Exception: If the deque is empty. 57 """ 58 return self.pop_left()
Pop an element from the front of the deque. (synonym for pop_left) Raises an exception if the deque is empty.
Returns: The leftmost element of the deque.
Raises: Exception: If the deque is empty.
60 def pop_back(self): 61 """ 62 Pop an element from the back of the deque. (synonym for pop_right) 63 Raises an exception if the deque is empty. 64 65 Returns: 66 The rightmost element of the deque. 67 68 Raises: 69 Exception: If the deque is empty. 70 """ 71 return self.pop_right()
Pop an element from the back of the deque. (synonym for pop_right) Raises an exception if the deque is empty.
Returns: The rightmost element of the deque.
Raises: Exception: If the deque is empty.
73 def front(self): 74 """ 75 Get the element at the front of the deque without removing it. 76 Raises an exception if the deque is empty. 77 78 Returns: 79 The leftmost element. 80 81 Raises: 82 Exception: If the deque is empty. 83 """ 84 return self.peek_left()
Get the element at the front of the deque without removing it. Raises an exception if the deque is empty.
Returns: The leftmost element.
Raises: Exception: If the deque is empty.
86 def back(self): 87 """ 88 Get the element at the back of the deque without removing it. 89 Raises an exception if the deque is empty. 90 91 Returns: 92 The rightmost element. 93 94 Raises: 95 Exception: If the deque is empty. 96 """ 97 return self.peek_right()
Get the element at the back of the deque without removing it. Raises an exception if the deque is empty.
Returns: The rightmost element.
Raises: Exception: If the deque is empty.
99 def append_left(self, element): 100 """ 101 Append an element to the left of the deque. Raises an exception when the deque is full. 102 103 Args: 104 element: The element to append. 105 106 Raises: 107 Exception: If the deque is full. 108 """ 109 if self.count >= self.capacity(): 110 raise Exception("Deque Full") 111 if self._left < 0: 112 self._left = self.capacity() - 1 113 self._array[self._left] = element 114 self._left -= 1 115 self.count += 1
Append an element to the left of the deque. Raises an exception when the deque is full.
Args: element: The element to append.
Raises: Exception: If the deque is full.
117 def append_right(self, element): 118 """ 119 Append an element to the right of the deque. Raises an exception when the deque is full. 120 121 Args: 122 element: The element to append. 123 124 Raises: 125 Exception: If the deque is full. 126 """ 127 if self.count >= self.capacity(): 128 raise Exception("Deque Full") 129 if self._right > self.capacity() - 1: 130 self._right = 0 131 self._array[self._right] = element 132 self._right += 1 133 self.count += 1
Append an element to the right of the deque. Raises an exception when the deque is full.
Args: element: The element to append.
Raises: Exception: If the deque is full.
135 def pop_left(self): 136 """ 137 Remove and return the element from the left of the deque. Raises an exception if the deque is empty. 138 139 Returns: 140 The leftmost element of the deque. 141 142 Raises: 143 Exception: If the deque is empty. 144 """ 145 if self.is_empty(): 146 raise Exception("Empty Deque") 147 148 element = self._array[self._left + 1] 149 self._left += 1 150 if self._left + 1 >= len(self._array): 151 self._left = -1 152 self.count -= 1 153 return element
Remove and return the element from the left of the deque. Raises an exception if the deque is empty.
Returns: The leftmost element of the deque.
Raises: Exception: If the deque is empty.
155 def pop_right(self): 156 """ 157 Pop an element from right the deque. Raise an exception if the deque is empty. 158 159 Returns: 160 The rightmost element of the deque. 161 162 Raises: 163 Exception: If the deque is empty. 164 """ 165 if self.is_empty(): 166 raise Exception("Empty Deque") 167 element = self._array[self._right - 1] 168 self.count -= 1 169 170 self._right -= 1 171 if self._right < 0: 172 self._right = len(self._array) - 1 173 return element
Pop an element from right the deque. Raise an exception if the deque is empty.
Returns: The rightmost element of the deque.
Raises: Exception: If the deque is empty.
175 def peek_left(self): 176 """ 177 Get the element at the left of the deque without removing it. 178 Raises an exception if the deque is empty. 179 180 Returns: 181 The leftmost element. 182 183 Raises: 184 Exception: If the deque is empty. 185 """ 186 if self.is_empty(): 187 raise Exception("Empty deque") 188 return self._array[self._left + 1]
Get the element at the left of the deque without removing it. Raises an exception if the deque is empty.
Returns: The leftmost element.
Raises: Exception: If the deque is empty.
190 def peek_right(self): 191 """ 192 Get the element at the right of the deque without removing it. 193 Raises an exception if the deque is empty. 194 195 Returns: 196 The rightmost element. 197 198 Raises: 199 Exception: If the deque is empty. 200 """ 201 if self.is_empty(): 202 raise Exception("Empty deque") 203 return self._array[self._right - 1]
Get the element at the right of the deque without removing it. Raises an exception if the deque is empty.
Returns: The rightmost element.
Raises: Exception: If the deque is empty.
205 def is_empty(self) -> bool: 206 """ 207 Return a Boolean on whether the deque is empty or not. 208 """ 209 return self.count == 0
Return a Boolean on whether the deque is empty or not.
211 @classmethod 212 def from_list(cls, alist: list): 213 """ 214 Create a deque from a given list. Raises an exception if list exceeds the deque's capacity. 215 216 Args: 217 alist: The list to initialize the deque with. 218 219 Returns: 220 a deque instance with elements from the list. 221 222 Raises: 223 Exception: If list exceeds the deque's capacity. 224 """ 225 dq = cls() 226 for e in alist: 227 dq.append_right(e) 228 return dq
Create a deque from a given list. Raises an exception if list exceeds the deque's capacity.
Args: alist: The list to initialize the deque with.
Returns: a deque instance with elements from the list.
Raises: Exception: If list exceeds the deque's capacity.
230 def to_list(self): 231 """ 232 Convert the deque's contents into a list. 233 234 Returns: 235 A list containg the elements in the deque. 236 """ 237 arr = [] 238 for i in range(self.count): 239 index = (i + self._left + 1) % len(self._array) 240 arr.append(self._array[index]) 241 return arr
Convert the deque's contents into a list.
Returns: A list containg the elements in the deque.
275class DynamicDeque(Deque): 276 """ 277 A dynamic deque implementation that adjusts its capacity as needed. 278 """ 279 def grow(self): 280 """ 281 Helper method to double the capacity of the deque. 282 """ 283 new_array = [ None ] * self.capacity() * 2 284 285 # copy elements 286 for i in range(self.count): 287 index = (i + self._left + 1) % len(self._array) 288 new_array[i] = self._array[index] 289 290 self._array = new_array 291 self._left = -1 292 self._right = self.count 293 294 def shrink(self): 295 """ 296 Helper method to halve the capacity of the deque. 297 """ 298 if self.capacity() < 10: 299 return 300 301 new_capacity = self.capacity() // 2 302 new_array = [ None ] * new_capacity 303 304 # copy elements 305 for i in range(self.count): 306 index = (i + self._left + 1) % len(self._array) 307 new_array[i] = self._array[index] 308 309 self._array = new_array 310 self._left = -1 311 self._right = self.count 312 313 314 def check_capacity(self): 315 """ 316 Helper method to adjust the capacity of the deque based on its count: 317 if count >= capacity, grow the deque 318 if count <= 1/4 of capacity, shrink the deque 319 """ 320 if self.count >= self.capacity(): 321 self.grow() 322 elif self.count * 4 <= self.capacity(): 323 self.shrink() 324 325 def append_left(self, element): 326 """ 327 Append an element to the left, adjusting capacity if needed. 328 329 Args: 330 element: The element to append. 331 """ 332 self.check_capacity() 333 super().append_left(element) 334 335 def append_right(self, element): 336 """ 337 Append an element to the right, adjusting capacity if needed. 338 339 Args: 340 element: The element to append. 341 """ 342 self.check_capacity() 343 super().append_right(element) 344 345 def pop_left(self): 346 """ 347 Remove and return the element from the left, adjusting capacity if needed. 348 349 Returns: 350 The leftmost element. 351 """ 352 self.check_capacity() 353 return super().pop_left() 354 355 356 def pop_right(self): 357 """ 358 Remove and return the element from the right, adjusting capacity if needed. 359 360 Returns: 361 The rightmost element. 362 """ 363 self.check_capacity() 364 return super().pop_right()
A dynamic deque implementation that adjusts its capacity as needed.
279 def grow(self): 280 """ 281 Helper method to double the capacity of the deque. 282 """ 283 new_array = [ None ] * self.capacity() * 2 284 285 # copy elements 286 for i in range(self.count): 287 index = (i + self._left + 1) % len(self._array) 288 new_array[i] = self._array[index] 289 290 self._array = new_array 291 self._left = -1 292 self._right = self.count
Helper method to double the capacity of the deque.
294 def shrink(self): 295 """ 296 Helper method to halve the capacity of the deque. 297 """ 298 if self.capacity() < 10: 299 return 300 301 new_capacity = self.capacity() // 2 302 new_array = [ None ] * new_capacity 303 304 # copy elements 305 for i in range(self.count): 306 index = (i + self._left + 1) % len(self._array) 307 new_array[i] = self._array[index] 308 309 self._array = new_array 310 self._left = -1 311 self._right = self.count
Helper method to halve the capacity of the deque.
314 def check_capacity(self): 315 """ 316 Helper method to adjust the capacity of the deque based on its count: 317 if count >= capacity, grow the deque 318 if count <= 1/4 of capacity, shrink the deque 319 """ 320 if self.count >= self.capacity(): 321 self.grow() 322 elif self.count * 4 <= self.capacity(): 323 self.shrink()
Helper method to adjust the capacity of the deque based on its count: if count >= capacity, grow the deque if count <= 1/4 of capacity, shrink the deque
325 def append_left(self, element): 326 """ 327 Append an element to the left, adjusting capacity if needed. 328 329 Args: 330 element: The element to append. 331 """ 332 self.check_capacity() 333 super().append_left(element)
Append an element to the left, adjusting capacity if needed.
Args: element: The element to append.
335 def append_right(self, element): 336 """ 337 Append an element to the right, adjusting capacity if needed. 338 339 Args: 340 element: The element to append. 341 """ 342 self.check_capacity() 343 super().append_right(element)
Append an element to the right, adjusting capacity if needed.
Args: element: The element to append.
345 def pop_left(self): 346 """ 347 Remove and return the element from the left, adjusting capacity if needed. 348 349 Returns: 350 The leftmost element. 351 """ 352 self.check_capacity() 353 return super().pop_left()
Remove and return the element from the left, adjusting capacity if needed.
Returns: The leftmost element.
356 def pop_right(self): 357 """ 358 Remove and return the element from the right, adjusting capacity if needed. 359 360 Returns: 361 The rightmost element. 362 """ 363 self.check_capacity() 364 return super().pop_right()
Remove and return the element from the right, adjusting capacity if needed.
Returns: The rightmost element.