dsa.array
Module containing array classes.
1""" Module containing array classes. """ 2 3class Array: 4 """ 5 A static array implementation. 6 7 Special Methods: 8 9 Index Operator: 10 array[index] 11 12 Assignment: 13 array[index] = value 14 """ 15 def __init__(self, contents=None, capacity: int=10): 16 """ 17 Initialize the array with optional contents and a fixed capacity. 18 19 Args: 20 contents: An optional iterable to fill array with default values. 21 capacity (int): The initial size of the array (default is 10) 22 """ 23 self._array = [ None ] * capacity 24 #: number of elements currently in array 25 self.count = 0 26 27 if contents: 28 self.extend(contents) 29 30 def append(self, element): 31 """ 32 Append an element to the array. Raise an exception if capacity is exceeded. 33 34 Args: 35 element: The element to append. 36 37 Raises: 38 Exception: If the array is full. 39 """ 40 if self.count >= self.capacity(): 41 raise Exception(f"Capacity Error: Maximum capacity {len(self)} reached.") 42 43 self._array[self.count] = element 44 self.count += 1 45 46 def extend(self, array): 47 """ 48 Append multiple elements from a given array. 49 50 Args: 51 array: An iterable containing elements to append. 52 53 Raises: 54 Exception: If appending the elements exceeds the array's capacity. 55 """ 56 for e in array: 57 self.append(e) 58 59 def insert(self, index: int, element): 60 """ 61 Insert an element at a specified index, shifting existing elements to the right. 62 63 Args: 64 index (int): The index at which to insert the element. 65 element: The element to insert. 66 67 Raises: 68 IndexError: If the index is out of bounds. 69 """ 70 if index == self.count: 71 self.append(element) 72 return 73 74 if index < 0 or index > self.count: 75 raise IndexError 76 77 self.shift_right(index) 78 self._array[index] = element 79 self.count += 1 80 81 def shift_right(self, start: int): 82 """ 83 Helper method to shift elements to the right from a specified start index until the last element. 84 (May delete an element but does not affect the count.) 85 Args: 86 start (int): The index at which to start shifting (inclusive). 87 88 Raises: 89 Exception: If the array is full and cannot accommodate the shift. 90 """ 91 if self.count >= len(self._array): 92 raise Exception(f"Capacity Error: Maximum capacity {len(self)} reached.") 93 end = self.count 94 for i in range(end, start, -1): 95 self._array[i] = self._array[i - 1] 96 97 def delete(self, index: int): 98 """ 99 Delete an element at a specified index, shifting subsequent elements to the left. 100 101 Args: 102 index (int): The index of the element to delete. 103 104 Raises: 105 IndexError: If index is out of bounds. 106 """ 107 if index < 0 or index >= self.count: 108 raise IndexError 109 110 self.shift_left(index) 111 self.count -= 1 112 113 def shift_left(self, start: int): 114 """ 115 Helper method to shift elements to the left starting at a start index. 116 (May delete an element but does not affect the count.) 117 118 Args: 119 start (int): The starting index of the shift. 120 """ 121 for i in range(start, self.count - 1): 122 self._array[i] = self._array[i + 1] 123 124 def __getitem__(self, index: int): 125 """ 126 Retrieve the element at the specified index. 127 128 Args: 129 index (int): The index of the element. 130 131 Returns: 132 The element at the specified index. 133 134 Raises: 135 IndexError: If the index is out of bounds. 136 """ 137 if index < 0 or index >= self.count: 138 raise IndexError 139 return self._array[index] 140 141 def __setitem__(self, index: int, value): 142 """ 143 Set a new value at the specified index. 144 145 Args: 146 index (int): The index at which to set the value. 147 value: The new value to assign. 148 149 Raises: 150 IndexError: If the index is out of bounds. 151 """ 152 if index < 0 or index >= self.count: 153 raise IndexError 154 self._array[index] = value 155 156 def __len__(self) -> int: 157 """ 158 Return the number of elements in the array. 159 160 Returns: 161 The number of elements in the array. 162 """ 163 return self.count 164 165 def is_empty(self) -> bool: 166 """ 167 Check if the array is empty. 168 169 Returns: 170 True if the array is empty, False otherwise. 171 """ 172 return self.count == 0 173 174 def capacity(self) -> int: 175 """ 176 Get the total capacity of the array. 177 178 Returns: 179 The capacity of the array. 180 """ 181 return len(self._array) 182 183 def to_list(self) -> list: 184 """ 185 Convert the array's elements to a standard Python list. 186 187 Returns: 188 A list containing the elements of the array. 189 """ 190 return self._array[:self.count] 191 192 @classmethod 193 def from_list(cls, mylist: list): 194 """ 195 Create an array from a standard Python list. 196 197 Args: 198 mylist: A Python list to initialize the array. 199 200 Returns: 201 An instance of the Array class. 202 """ 203 list_instance = cls() 204 list_instance.extend(mylist) 205 206 return list_instance 207 208 def __repr__(self): 209 """ 210 Represent the array's contents, count, and capacity. 211 212 Returns: 213 A string representation of the array. 214 """ 215 return f'{self.to_list()} Count: {self.count} Capacity: {self.capacity()}' 216 217class DynamicArray(Array): 218 """ 219 A dynamic array implementation. Capacity will adjust as needed. 220 221 Special Methods: 222 223 Index Operator: 224 array[index] 225 226 Assignment: 227 array[index] = value 228 """ 229 230 def grow(self): 231 """ 232 Helper method to double the capacity of the current array. 233 """ 234 new_size = len(self._array) * 2 235 new_array = [ None ] * new_size 236 237 # copy elements 238 for i in range(len(self._array)): 239 new_array[i] = self._array[i] 240 241 self._array = new_array 242 243 def shrink(self): 244 """ 245 Helper method to halve the capacity of the current array. 246 """ 247 new_size = len(self._array) // 2 248 new_array = [ None ] * new_size 249 250 # copy elements 251 for i in range(new_size): 252 new_array[i] = self._array[i] 253 254 self._array = new_array 255 256 def check_capacity(self): 257 """ 258 if count >= capacity, grow the array. 259 if count <= 1/4 of capacity, shrink the array. 260 """ 261 if self.count >= len(self._array): 262 self.grow() 263 elif self.count * 4 <= len(self._array): 264 self.shrink() 265 266 def append(self, element): 267 """ 268 Append an element to the array. Adjust the capacity as needed. 269 270 Args: 271 element: The element to append. 272 """ 273 self.check_capacity() 274 275 self._array[self.count] = element 276 self.count += 1 277 278 def extend(self, array): 279 """ 280 Append multiple elements from a given array. Adjust the capacity as needed. 281 282 Args: 283 array: An iterable containing elements to append. 284 """ 285 for e in array: 286 self.append(e) 287 288 def insert(self, index: int, element): 289 """ 290 Insert an element at a specified index, shifting existing elements to the right. Adjust the capacity as needed. 291 292 Args: 293 index (int): The index at which to insert the element. 294 element: The element to insert. 295 """ 296 if index >= self.count or index < 0: 297 raise IndexError 298 299 self.check_capacity() 300 301 self.shift_right(index) 302 self._array[index] = element 303 self.count += 1 304 305 def delete(self, index: int): 306 """ 307 Delete an element at a specified index, shifting subsequent elements to the left. Adjust the capacity as needed. 308 309 Args: 310 index (int): The index of the element to delete. 311 """ 312 if index >= self.count or index < 0: 313 raise IndexError 314 315 self.check_capacity() 316 317 self.shift_left(index) 318 self.count -= 1 319 320 321class CircularArray(Array): 322 """ 323 A circular array implementation. 324 325 Special Methods: 326 327 Index Operator: 328 array[index] 329 330 Assignment: 331 array[index] = value 332 """ 333 def __init__(self, contents=None, capacity: int=10): 334 """ 335 Initialize the circular array with optional contents and a fixed capacity. 336 337 Args: 338 contents: An optional iterable to fill array with default values. 339 capacity (int): The initial size of the array (default is 10) 340 """ 341 super().__init__(None, capacity) 342 #: index of the first element in the circular array 343 self._start = 0 344 if contents: 345 self.extend(contents) 346 347 def __getitem__(self, index: int): 348 """ 349 Retrieve the element at the specified index. 350 351 Args: 352 index (int): The index of the element. 353 354 Returns: 355 The element at the specified index. 356 357 Raises: 358 IndexError: If the index is out of bounds. 359 """ 360 if index < 0 or index >= self.count: 361 raise IndexError 362 return self._array[(self._start + index) % len(self._array)] 363 364 def __setitem__(self, index: int, value): 365 """ 366 Set a new value at the specified index. 367 368 Args: 369 index (int): The index at which to set the value. 370 value: The new value to assign. 371 372 Raises: 373 IndexError: If the index is out of bounds. 374 """ 375 if index < 0 or index >= self.count: 376 raise IndexError 377 self._array[(self._start + index) % len(self._array)] = value 378 379 def append(self, element): 380 """ 381 Append an element to the circular array. If appending exceeds capacity, it will wrap around to the oldest element. 382 383 Args: 384 element: The element to append. 385 """ 386 # self._array[(self._start + self.count) % len(self._array)] = element 387 # if self.count < self.capacity(): 388 # self.count += 1 389 # else: 390 # self._start = (self._start + 1) % len(self._array) 391 index = (self._start + self.count) % len(self._array) 392 self._array[index] = element 393 394 if self.count < self.capacity(): 395 self.count += 1 396 else: 397 self._start = (self._start + 1) % len(self._array) # Overwrite oldest element 398 399 def raw_view(self): 400 """ 401 Return a raw view of the array. 402 403 Returns: 404 A raw view of the array. 405 """ 406 return self._array 407 408 def to_list(self): 409 """ 410 Convert the array's elements to a standard Python list. 411 412 Returns: 413 A list containing the elements of the array. 414 """ 415 output_list = [] 416 for i in range(self.count): 417 output_list.append(self._array[(self._start + i) % len(self._array)]) 418 return output_list 419 420 def insert(self, index: int, element): 421 """ 422 not yet implemented 423 """ 424 pass 425 426 def delete(self, index: int): 427 """ 428 not yet implemented 429 """ 430 pass
4class Array: 5 """ 6 A static array implementation. 7 8 Special Methods: 9 10 Index Operator: 11 array[index] 12 13 Assignment: 14 array[index] = value 15 """ 16 def __init__(self, contents=None, capacity: int=10): 17 """ 18 Initialize the array with optional contents and a fixed capacity. 19 20 Args: 21 contents: An optional iterable to fill array with default values. 22 capacity (int): The initial size of the array (default is 10) 23 """ 24 self._array = [ None ] * capacity 25 #: number of elements currently in array 26 self.count = 0 27 28 if contents: 29 self.extend(contents) 30 31 def append(self, element): 32 """ 33 Append an element to the array. Raise an exception if capacity is exceeded. 34 35 Args: 36 element: The element to append. 37 38 Raises: 39 Exception: If the array is full. 40 """ 41 if self.count >= self.capacity(): 42 raise Exception(f"Capacity Error: Maximum capacity {len(self)} reached.") 43 44 self._array[self.count] = element 45 self.count += 1 46 47 def extend(self, array): 48 """ 49 Append multiple elements from a given array. 50 51 Args: 52 array: An iterable containing elements to append. 53 54 Raises: 55 Exception: If appending the elements exceeds the array's capacity. 56 """ 57 for e in array: 58 self.append(e) 59 60 def insert(self, index: int, element): 61 """ 62 Insert an element at a specified index, shifting existing elements to the right. 63 64 Args: 65 index (int): The index at which to insert the element. 66 element: The element to insert. 67 68 Raises: 69 IndexError: If the index is out of bounds. 70 """ 71 if index == self.count: 72 self.append(element) 73 return 74 75 if index < 0 or index > self.count: 76 raise IndexError 77 78 self.shift_right(index) 79 self._array[index] = element 80 self.count += 1 81 82 def shift_right(self, start: int): 83 """ 84 Helper method to shift elements to the right from a specified start index until the last element. 85 (May delete an element but does not affect the count.) 86 Args: 87 start (int): The index at which to start shifting (inclusive). 88 89 Raises: 90 Exception: If the array is full and cannot accommodate the shift. 91 """ 92 if self.count >= len(self._array): 93 raise Exception(f"Capacity Error: Maximum capacity {len(self)} reached.") 94 end = self.count 95 for i in range(end, start, -1): 96 self._array[i] = self._array[i - 1] 97 98 def delete(self, index: int): 99 """ 100 Delete an element at a specified index, shifting subsequent elements to the left. 101 102 Args: 103 index (int): The index of the element to delete. 104 105 Raises: 106 IndexError: If index is out of bounds. 107 """ 108 if index < 0 or index >= self.count: 109 raise IndexError 110 111 self.shift_left(index) 112 self.count -= 1 113 114 def shift_left(self, start: int): 115 """ 116 Helper method to shift elements to the left starting at a start index. 117 (May delete an element but does not affect the count.) 118 119 Args: 120 start (int): The starting index of the shift. 121 """ 122 for i in range(start, self.count - 1): 123 self._array[i] = self._array[i + 1] 124 125 def __getitem__(self, index: int): 126 """ 127 Retrieve the element at the specified index. 128 129 Args: 130 index (int): The index of the element. 131 132 Returns: 133 The element at the specified index. 134 135 Raises: 136 IndexError: If the index is out of bounds. 137 """ 138 if index < 0 or index >= self.count: 139 raise IndexError 140 return self._array[index] 141 142 def __setitem__(self, index: int, value): 143 """ 144 Set a new value at the specified index. 145 146 Args: 147 index (int): The index at which to set the value. 148 value: The new value to assign. 149 150 Raises: 151 IndexError: If the index is out of bounds. 152 """ 153 if index < 0 or index >= self.count: 154 raise IndexError 155 self._array[index] = value 156 157 def __len__(self) -> int: 158 """ 159 Return the number of elements in the array. 160 161 Returns: 162 The number of elements in the array. 163 """ 164 return self.count 165 166 def is_empty(self) -> bool: 167 """ 168 Check if the array is empty. 169 170 Returns: 171 True if the array is empty, False otherwise. 172 """ 173 return self.count == 0 174 175 def capacity(self) -> int: 176 """ 177 Get the total capacity of the array. 178 179 Returns: 180 The capacity of the array. 181 """ 182 return len(self._array) 183 184 def to_list(self) -> list: 185 """ 186 Convert the array's elements to a standard Python list. 187 188 Returns: 189 A list containing the elements of the array. 190 """ 191 return self._array[:self.count] 192 193 @classmethod 194 def from_list(cls, mylist: list): 195 """ 196 Create an array from a standard Python list. 197 198 Args: 199 mylist: A Python list to initialize the array. 200 201 Returns: 202 An instance of the Array class. 203 """ 204 list_instance = cls() 205 list_instance.extend(mylist) 206 207 return list_instance 208 209 def __repr__(self): 210 """ 211 Represent the array's contents, count, and capacity. 212 213 Returns: 214 A string representation of the array. 215 """ 216 return f'{self.to_list()} Count: {self.count} Capacity: {self.capacity()}'
A static array implementation.
Special Methods:
Index Operator:
array[index]
Assignment:
array[index] = value
16 def __init__(self, contents=None, capacity: int=10): 17 """ 18 Initialize the array with optional contents and a fixed capacity. 19 20 Args: 21 contents: An optional iterable to fill array with default values. 22 capacity (int): The initial size of the array (default is 10) 23 """ 24 self._array = [ None ] * capacity 25 #: number of elements currently in array 26 self.count = 0 27 28 if contents: 29 self.extend(contents)
Initialize the array with optional contents and a fixed capacity.
Args: contents: An optional iterable to fill array with default values. capacity (int): The initial size of the array (default is 10)
31 def append(self, element): 32 """ 33 Append an element to the array. Raise an exception if capacity is exceeded. 34 35 Args: 36 element: The element to append. 37 38 Raises: 39 Exception: If the array is full. 40 """ 41 if self.count >= self.capacity(): 42 raise Exception(f"Capacity Error: Maximum capacity {len(self)} reached.") 43 44 self._array[self.count] = element 45 self.count += 1
Append an element to the array. Raise an exception if capacity is exceeded.
Args: element: The element to append.
Raises: Exception: If the array is full.
47 def extend(self, array): 48 """ 49 Append multiple elements from a given array. 50 51 Args: 52 array: An iterable containing elements to append. 53 54 Raises: 55 Exception: If appending the elements exceeds the array's capacity. 56 """ 57 for e in array: 58 self.append(e)
Append multiple elements from a given array.
Args: array: An iterable containing elements to append.
Raises: Exception: If appending the elements exceeds the array's capacity.
60 def insert(self, index: int, element): 61 """ 62 Insert an element at a specified index, shifting existing elements to the right. 63 64 Args: 65 index (int): The index at which to insert the element. 66 element: The element to insert. 67 68 Raises: 69 IndexError: If the index is out of bounds. 70 """ 71 if index == self.count: 72 self.append(element) 73 return 74 75 if index < 0 or index > self.count: 76 raise IndexError 77 78 self.shift_right(index) 79 self._array[index] = element 80 self.count += 1
Insert an element at a specified index, shifting existing elements to the right.
Args: index (int): The index at which to insert the element. element: The element to insert.
Raises: IndexError: If the index is out of bounds.
82 def shift_right(self, start: int): 83 """ 84 Helper method to shift elements to the right from a specified start index until the last element. 85 (May delete an element but does not affect the count.) 86 Args: 87 start (int): The index at which to start shifting (inclusive). 88 89 Raises: 90 Exception: If the array is full and cannot accommodate the shift. 91 """ 92 if self.count >= len(self._array): 93 raise Exception(f"Capacity Error: Maximum capacity {len(self)} reached.") 94 end = self.count 95 for i in range(end, start, -1): 96 self._array[i] = self._array[i - 1]
Helper method to shift elements to the right from a specified start index until the last element. (May delete an element but does not affect the count.) Args: start (int): The index at which to start shifting (inclusive).
Raises: Exception: If the array is full and cannot accommodate the shift.
98 def delete(self, index: int): 99 """ 100 Delete an element at a specified index, shifting subsequent elements to the left. 101 102 Args: 103 index (int): The index of the element to delete. 104 105 Raises: 106 IndexError: If index is out of bounds. 107 """ 108 if index < 0 or index >= self.count: 109 raise IndexError 110 111 self.shift_left(index) 112 self.count -= 1
Delete an element at a specified index, shifting subsequent elements to the left.
Args: index (int): The index of the element to delete.
Raises:
IndexError: If index is out of bounds.
114 def shift_left(self, start: int): 115 """ 116 Helper method to shift elements to the left starting at a start index. 117 (May delete an element but does not affect the count.) 118 119 Args: 120 start (int): The starting index of the shift. 121 """ 122 for i in range(start, self.count - 1): 123 self._array[i] = self._array[i + 1]
Helper method to shift elements to the left starting at a start index. (May delete an element but does not affect the count.)
Args: start (int): The starting index of the shift.
166 def is_empty(self) -> bool: 167 """ 168 Check if the array is empty. 169 170 Returns: 171 True if the array is empty, False otherwise. 172 """ 173 return self.count == 0
Check if the array is empty.
Returns: True if the array is empty, False otherwise.
175 def capacity(self) -> int: 176 """ 177 Get the total capacity of the array. 178 179 Returns: 180 The capacity of the array. 181 """ 182 return len(self._array)
Get the total capacity of the array.
Returns: The capacity of the array.
184 def to_list(self) -> list: 185 """ 186 Convert the array's elements to a standard Python list. 187 188 Returns: 189 A list containing the elements of the array. 190 """ 191 return self._array[:self.count]
Convert the array's elements to a standard Python list.
Returns: A list containing the elements of the array.
193 @classmethod 194 def from_list(cls, mylist: list): 195 """ 196 Create an array from a standard Python list. 197 198 Args: 199 mylist: A Python list to initialize the array. 200 201 Returns: 202 An instance of the Array class. 203 """ 204 list_instance = cls() 205 list_instance.extend(mylist) 206 207 return list_instance
Create an array from a standard Python list.
Args: mylist: A Python list to initialize the array.
Returns: An instance of the Array class.
218class DynamicArray(Array): 219 """ 220 A dynamic array implementation. Capacity will adjust as needed. 221 222 Special Methods: 223 224 Index Operator: 225 array[index] 226 227 Assignment: 228 array[index] = value 229 """ 230 231 def grow(self): 232 """ 233 Helper method to double the capacity of the current array. 234 """ 235 new_size = len(self._array) * 2 236 new_array = [ None ] * new_size 237 238 # copy elements 239 for i in range(len(self._array)): 240 new_array[i] = self._array[i] 241 242 self._array = new_array 243 244 def shrink(self): 245 """ 246 Helper method to halve the capacity of the current array. 247 """ 248 new_size = len(self._array) // 2 249 new_array = [ None ] * new_size 250 251 # copy elements 252 for i in range(new_size): 253 new_array[i] = self._array[i] 254 255 self._array = new_array 256 257 def check_capacity(self): 258 """ 259 if count >= capacity, grow the array. 260 if count <= 1/4 of capacity, shrink the array. 261 """ 262 if self.count >= len(self._array): 263 self.grow() 264 elif self.count * 4 <= len(self._array): 265 self.shrink() 266 267 def append(self, element): 268 """ 269 Append an element to the array. Adjust the capacity as needed. 270 271 Args: 272 element: The element to append. 273 """ 274 self.check_capacity() 275 276 self._array[self.count] = element 277 self.count += 1 278 279 def extend(self, array): 280 """ 281 Append multiple elements from a given array. Adjust the capacity as needed. 282 283 Args: 284 array: An iterable containing elements to append. 285 """ 286 for e in array: 287 self.append(e) 288 289 def insert(self, index: int, element): 290 """ 291 Insert an element at a specified index, shifting existing elements to the right. Adjust the capacity as needed. 292 293 Args: 294 index (int): The index at which to insert the element. 295 element: The element to insert. 296 """ 297 if index >= self.count or index < 0: 298 raise IndexError 299 300 self.check_capacity() 301 302 self.shift_right(index) 303 self._array[index] = element 304 self.count += 1 305 306 def delete(self, index: int): 307 """ 308 Delete an element at a specified index, shifting subsequent elements to the left. Adjust the capacity as needed. 309 310 Args: 311 index (int): The index of the element to delete. 312 """ 313 if index >= self.count or index < 0: 314 raise IndexError 315 316 self.check_capacity() 317 318 self.shift_left(index) 319 self.count -= 1
A dynamic array implementation. Capacity will adjust as needed.
Special Methods:
Index Operator:
array[index]
Assignment:
array[index] = value
231 def grow(self): 232 """ 233 Helper method to double the capacity of the current array. 234 """ 235 new_size = len(self._array) * 2 236 new_array = [ None ] * new_size 237 238 # copy elements 239 for i in range(len(self._array)): 240 new_array[i] = self._array[i] 241 242 self._array = new_array
Helper method to double the capacity of the current array.
244 def shrink(self): 245 """ 246 Helper method to halve the capacity of the current array. 247 """ 248 new_size = len(self._array) // 2 249 new_array = [ None ] * new_size 250 251 # copy elements 252 for i in range(new_size): 253 new_array[i] = self._array[i] 254 255 self._array = new_array
Helper method to halve the capacity of the current array.
257 def check_capacity(self): 258 """ 259 if count >= capacity, grow the array. 260 if count <= 1/4 of capacity, shrink the array. 261 """ 262 if self.count >= len(self._array): 263 self.grow() 264 elif self.count * 4 <= len(self._array): 265 self.shrink()
if count >= capacity, grow the array. if count <= 1/4 of capacity, shrink the array.
267 def append(self, element): 268 """ 269 Append an element to the array. Adjust the capacity as needed. 270 271 Args: 272 element: The element to append. 273 """ 274 self.check_capacity() 275 276 self._array[self.count] = element 277 self.count += 1
Append an element to the array. Adjust the capacity as needed.
Args: element: The element to append.
279 def extend(self, array): 280 """ 281 Append multiple elements from a given array. Adjust the capacity as needed. 282 283 Args: 284 array: An iterable containing elements to append. 285 """ 286 for e in array: 287 self.append(e)
Append multiple elements from a given array. Adjust the capacity as needed.
Args: array: An iterable containing elements to append.
289 def insert(self, index: int, element): 290 """ 291 Insert an element at a specified index, shifting existing elements to the right. Adjust the capacity as needed. 292 293 Args: 294 index (int): The index at which to insert the element. 295 element: The element to insert. 296 """ 297 if index >= self.count or index < 0: 298 raise IndexError 299 300 self.check_capacity() 301 302 self.shift_right(index) 303 self._array[index] = element 304 self.count += 1
Insert an element at a specified index, shifting existing elements to the right. Adjust the capacity as needed.
Args: index (int): The index at which to insert the element. element: The element to insert.
306 def delete(self, index: int): 307 """ 308 Delete an element at a specified index, shifting subsequent elements to the left. Adjust the capacity as needed. 309 310 Args: 311 index (int): The index of the element to delete. 312 """ 313 if index >= self.count or index < 0: 314 raise IndexError 315 316 self.check_capacity() 317 318 self.shift_left(index) 319 self.count -= 1
Delete an element at a specified index, shifting subsequent elements to the left. Adjust the capacity as needed.
Args: index (int): The index of the element to delete.
Inherited Members
322class CircularArray(Array): 323 """ 324 A circular array implementation. 325 326 Special Methods: 327 328 Index Operator: 329 array[index] 330 331 Assignment: 332 array[index] = value 333 """ 334 def __init__(self, contents=None, capacity: int=10): 335 """ 336 Initialize the circular array with optional contents and a fixed capacity. 337 338 Args: 339 contents: An optional iterable to fill array with default values. 340 capacity (int): The initial size of the array (default is 10) 341 """ 342 super().__init__(None, capacity) 343 #: index of the first element in the circular array 344 self._start = 0 345 if contents: 346 self.extend(contents) 347 348 def __getitem__(self, index: int): 349 """ 350 Retrieve the element at the specified index. 351 352 Args: 353 index (int): The index of the element. 354 355 Returns: 356 The element at the specified index. 357 358 Raises: 359 IndexError: If the index is out of bounds. 360 """ 361 if index < 0 or index >= self.count: 362 raise IndexError 363 return self._array[(self._start + index) % len(self._array)] 364 365 def __setitem__(self, index: int, value): 366 """ 367 Set a new value at the specified index. 368 369 Args: 370 index (int): The index at which to set the value. 371 value: The new value to assign. 372 373 Raises: 374 IndexError: If the index is out of bounds. 375 """ 376 if index < 0 or index >= self.count: 377 raise IndexError 378 self._array[(self._start + index) % len(self._array)] = value 379 380 def append(self, element): 381 """ 382 Append an element to the circular array. If appending exceeds capacity, it will wrap around to the oldest element. 383 384 Args: 385 element: The element to append. 386 """ 387 # self._array[(self._start + self.count) % len(self._array)] = element 388 # if self.count < self.capacity(): 389 # self.count += 1 390 # else: 391 # self._start = (self._start + 1) % len(self._array) 392 index = (self._start + self.count) % len(self._array) 393 self._array[index] = element 394 395 if self.count < self.capacity(): 396 self.count += 1 397 else: 398 self._start = (self._start + 1) % len(self._array) # Overwrite oldest element 399 400 def raw_view(self): 401 """ 402 Return a raw view of the array. 403 404 Returns: 405 A raw view of the array. 406 """ 407 return self._array 408 409 def to_list(self): 410 """ 411 Convert the array's elements to a standard Python list. 412 413 Returns: 414 A list containing the elements of the array. 415 """ 416 output_list = [] 417 for i in range(self.count): 418 output_list.append(self._array[(self._start + i) % len(self._array)]) 419 return output_list 420 421 def insert(self, index: int, element): 422 """ 423 not yet implemented 424 """ 425 pass 426 427 def delete(self, index: int): 428 """ 429 not yet implemented 430 """ 431 pass
A circular array implementation.
Special Methods:
Index Operator:
array[index]
Assignment:
array[index] = value
334 def __init__(self, contents=None, capacity: int=10): 335 """ 336 Initialize the circular array with optional contents and a fixed capacity. 337 338 Args: 339 contents: An optional iterable to fill array with default values. 340 capacity (int): The initial size of the array (default is 10) 341 """ 342 super().__init__(None, capacity) 343 #: index of the first element in the circular array 344 self._start = 0 345 if contents: 346 self.extend(contents)
Initialize the circular array with optional contents and a fixed capacity.
Args: contents: An optional iterable to fill array with default values. capacity (int): The initial size of the array (default is 10)
380 def append(self, element): 381 """ 382 Append an element to the circular array. If appending exceeds capacity, it will wrap around to the oldest element. 383 384 Args: 385 element: The element to append. 386 """ 387 # self._array[(self._start + self.count) % len(self._array)] = element 388 # if self.count < self.capacity(): 389 # self.count += 1 390 # else: 391 # self._start = (self._start + 1) % len(self._array) 392 index = (self._start + self.count) % len(self._array) 393 self._array[index] = element 394 395 if self.count < self.capacity(): 396 self.count += 1 397 else: 398 self._start = (self._start + 1) % len(self._array) # Overwrite oldest element
Append an element to the circular array. If appending exceeds capacity, it will wrap around to the oldest element.
Args: element: The element to append.
400 def raw_view(self): 401 """ 402 Return a raw view of the array. 403 404 Returns: 405 A raw view of the array. 406 """ 407 return self._array
Return a raw view of the array.
Returns: A raw view of the array.
409 def to_list(self): 410 """ 411 Convert the array's elements to a standard Python list. 412 413 Returns: 414 A list containing the elements of the array. 415 """ 416 output_list = [] 417 for i in range(self.count): 418 output_list.append(self._array[(self._start + i) % len(self._array)]) 419 return output_list
Convert the array's elements to a standard Python list.
Returns: A list containing the elements of the array.