Array
Download Source Files
- Download and place the following in your datastructures folder:
- Download and place the following in your tests folder:
Specification
This module defines an Array interface that represents a one-dimensional array. This file lists the stipulations and more information on the methods and their expected behavior. YOU SHOULD NOT MODIFY THIS FILE. Implement the Array class in the array.py file.
IArray
Bases: Sequence[T], Generic[T], ABC
Array representing a one-dimensional array that grows dynamically. Supports the bracket operator for getting and setting items in the array, the length operator, the equality and non-equality operators, the iterator and reversed iterator operators, the delete operator, the contains operator, the clear method, the string representation, and the resize method.
Source code in src/datastructures/iarray.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
__contains__(item)
abstractmethod
Contains operator (in). Checks if the array contains the item.
Examples:
>>> array = Array[str](starting_sequence=['zero', 'one', 'two', 'three', 'four'], data_type=str)
>>> print('three' in array)
True
>>> print('five' in array)
False
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Any
|
the desired item to check whether it's in the array. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
contains_item |
bool
|
true if the array contains the item. |
Source code in src/datastructures/iarray.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
__delitem__(index)
abstractmethod
Delete an item in the array. Copies the array contents from index + 1 down to fill the gap caused by deleting the item and shrinks the array size down by one. The algorithm should shrink the array physical size when the number of items in the array (logical size) is less than or equal to 1/4 of the physical size.
Examples:
>>> array = Array[str](starting_sequence=['zero', 'one', 'two', 'three', 'four'], data_type=str)
>>> print(repr(array))
Array(logical size: 5, items: ['zero', 'one', 'two', 'three', 'four'], physical size: 5, data type: <class 'str'>)
>>> del array[2]
>>> print(repr(array))
Array(logical size: 4, items: ['zero', 'one', 'three', 'four'], physical size: 5, data type: <class 'str'>)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
the desired index to delete. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/datastructures/iarray.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
__eq__(other)
abstractmethod
Equality operator == to check if two Arrays are equal (deep check).
Examples:
>>> array1 = Array[int](starting_sequence=[0, 1, 2, 3, 4], data_type=int)
>>> array2 = Array[int](starting_sequence=[0, 1, 2, 3, 4], data_type=int)
>>> print(array1 == array2)
True
array3 = Array[int](starting_sequence=[0, 1, 2, 3, 5], data_type=int)
>>> print(array1 == array3)
False
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
object
|
the instance to compare self to. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
is_equal |
bool
|
true if the arrays are equal (deep check). |
Source code in src/datastructures/iarray.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
__getitem__(index)
abstractmethod
__getitem__(index: int) -> T
__getitem__(index: slice) -> Sequence[T]
Bracket operator for getting an item (via int) or items (via slice) in an Array. If index is an integer, return the item at the index. If index is a slice, return the items at the slice. Supports wrap-around indexing via negative indixes.
Examples:
>>> array = Array[str](starting_sequence=['zero', 'one' , 'two', 'three', 'four'], data_type=str)
>>> print(repr(str(array[1]))) # invokes __getitem__ with an `int` for the index.
'one'
>>> print(array[1:3]) # invokes __getitem__ with a `slice` for the index.
['one', 'two']
>>> array = Array[str](starting_sequence=['zero', 'one', 'two', 'three', 'four'], data_type=str)
>>> print(repr(str(array[-1]))) # invokes __getitem__ with a negative `int` for the index.
'four'
>>> print(array[-3:]) # invokes __getitem__ with a `slice` for the index.
['two', 'three', 'four']
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | slice
|
the desired index or slice to get. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
item |
T | Sequence[T]
|
the item or items at index or slice. Items are of type T and slices are returned as list[T]. |
Raises:
| Type | Description |
|---|---|
IndexError
|
if the index is out of bounds. |
TypeError
|
if the index is not an integer or slice. |
Source code in src/datastructures/iarray.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
__init__(starting_sequence=[], data_type=object)
abstractmethod
Array Constructor. Initializes the Array using a Sequence type (anything that supports the bracket operator like a Python list). The Sequence should contain elements of the same type specified by the second parameter data_type. The underlying structure in the Array must be a NumPy Array. Internally, the Array should also manage a physical size (the size of the internal numpy array) and a logical size (the number of items in the Array).
Examples:
>>> array = Array[int](starting_sequence=[], data_type=int)
>>> print(repr(array))
Array(logical size: 0, items: [], physical size: 0, data type: <class 'int'>)
>>> array = Array[int](starting_sequence=[1, 2, 3, 4, 5], data_type=int)
>>> print(repr(array))
Array(logical size: 5, items: [1, 2, 3, 4, 5], physical size: 5, data type: <class 'int'>)
>>> array = Array[int]([num for num in range(15)], data_type=int)
>>> print(repr(array))
Array(logical size: 15, items: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], physical size: 15, data type: <class 'int'>)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sequence
|
Sequence[T]
|
the desired sequence type to initialize the Array with. |
required |
data_type
|
type
|
the desired data type to initialize the Array with (default=object). |
object
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
ValueError
|
if sequence is not a valid sequence type. |
ValueError
|
if data_type is not a valid data type. |
Source code in src/datastructures/iarray.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
__iter__()
abstractmethod
Iterator operator. Allows for iteration over the Array.
Examples:
>>> array = Array[str](starting_sequence=['one', 'two', 'three', 'four', 'five'], data_type=str)
>>> for item in array: # invokes __iter__
... print(repr(str(item)), end= ' ')
'one' 'two' 'three' 'four' 'five'
Yields:
| Name | Type | Description |
|---|---|---|
item |
T
|
yields the item at index |
Source code in src/datastructures/iarray.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
__len__()
abstractmethod
Length operator for getting the logical length (size) of the Array (number of items in the Array).
Examples:
>>> array = Array[int](starting_sequence=[num for num in range(10)], data_type=int)
>>> print(len(array))
10
Returns:
| Name | Type | Description |
|---|---|---|
length |
int
|
the length of the Array. |
Source code in src/datastructures/iarray.py
237 238 239 240 241 242 243 244 245 246 247 248 249 | |
__repr__()
abstractmethod
Return a programmer's representation of the data and structure.
Examples:
>>> array = Array[int](starting_sequence=[num for num in range(10)], data_type=int)
>>> print(repr(array))
Array(logical size: 10, items: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], physical size: 10, data type: <class 'int'>)
>>> array = Array[str](starting_sequence=['zero', 'one', 'two', 'three', 'four'], data_type=str)
>>> print(repr(array))
Array(logical size: 5, items: ['zero', 'one', 'two', 'three', 'four'], physical size: 5, data type: <class 'str'>)
Returns:
| Name | Type | Description |
|---|---|---|
string |
str
|
the programmer's representation of the data and structure. |
Source code in src/datastructures/iarray.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
__reversed__()
abstractmethod
Reversed iterator operator. Allows for iteration over the Array in reverse.
Examples:
>>> array = Array[str](starting_sequence=['one', 'two', 'three', 'four', 'five'], data_type=str)
>>> for item in reversed(array):
... print(repr(str(item)), end= ' ')
'five' 'four' 'three' 'two' 'one'
Yields:
| Name | Type | Description |
|---|---|---|
item |
T
|
yields the item at index starting at the end |
Source code in src/datastructures/iarray.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
__setitem__(index, item)
abstractmethod
Bracket operator for setting an item in an Array.
Examples:
>>> array = Array[int](starting_sequence=[50], data_type=int)
>>> array[0] = 10 # invokes __setitem__ with 0 as the index and 10 as the item to store at that index.
>>> print(array[0])
10
>>> print(array)
[10]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
the desired index to set. |
required |
item
|
T
|
the desired item to set at index. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
IndexError
|
if the index is out of bounds. |
TypeError
|
if the item is not the same type as the Array. |
Source code in src/datastructures/iarray.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
__str__()
abstractmethod
Return a string representation of the data and structure.
Examples:
>>> array = Array[int](starting_sequence=[num for num in range(10)], data_type=int)
>>> print(str(array))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> array = Array[str](starting_sequence=['zero', 'one', 'two', 'three', 'four'], data_type=str)
>>> print(str(array)))
[zero, one, two, three, four]
Returns:
| Name | Type | Description |
|---|---|---|
string |
str
|
the string representation of the data and structure. |
Source code in src/datastructures/iarray.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
append(data)
abstractmethod
Append an item to the end of the Array. Internally, the Array should manage a physical size (the size of the internal numpy array) and a logical size (the number of items in the Array). The algorithm should double the array physical size when the number of items in the array (logical size) is equal to the physical size.
Examples:
>>> array = Array[int](starting_sequence=[], data_type=int)
>>> print(repr(array))
Array(logical size: 0, items: [], physical size: 0, data type: <class 'int'>)
>>> for i in range(10):
... array.append(i)
... print(repr(array))
Array(logical size: 0, items: [], physical size: 0, data type: <class 'int'>)
Array(logical size: 1, items: [0], physical size: 2, data type: <class 'int'>)
Array(logical size: 2, items: [0, 1], physical size: 2, data type: <class 'int'>)
Array(logical size: 3, items: [0, 1, 2], physical size: 4, data type: <class 'int'>)
Array(logical size: 4, items: [0, 1, 2, 3], physical size: 4, data type: <class 'int'>)
Array(logical size: 5, items: [0, 1, 2, 3, 4], physical size: 8, data type: <class 'int'>)
Array(logical size: 6, items: [0, 1, 2, 3, 4, 5], physical size: 8, data type: <class 'int'>)
Array(logical size: 7, items: [0, 1, 2, 3, 4, 5, 6], physical size: 8, data type: <class 'int'>)
Array(logical size: 8, items: [0, 1, 2, 3, 4, 5, 6, 7], physical size: 8, data type: <class 'int'>)
Array(logical size: 9, items: [0, 1, 2, 3, 4, 5, 6, 7, 8], physical size: 16, data type: <class 'int'>)
Array(logical size: 10, items: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], physical size: 16, data type: <class 'int'>)
Args: data (T): the desired data to append.
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/datastructures/iarray.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
append_front(data)
abstractmethod
Append an item to the front of the Array. Internally, the Array should manage a physical size (the size of the internal numpy array) and a logical size (the number of items in the Array). The algorithm should double the array physical size when the number of items in the array (logical size) is equal to the physical size.
Examples:
>>> array = Array[int](starting_sequence=[], data_type=int)
>>> print(repr(array))
Array(logical size: 0, items: [], physical size: 0, data type: <class 'int'>)
>>> for num in range(10, 0, -1):
... array.append_front(i)
... print(repr(array))
Array(logical size: 1, items: [10], physical size: 2, data type: <class 'int'>)
Array(logical size: 2, items: [9, 10], physical size: 2, data type: <class 'int'>)
Array(logical size: 3, items: [8, 9, 10], physical size: 4, data type: <class 'int'>)
Array(logical size: 4, items: [7, 8, 9, 10], physical size: 4, data type: <class 'int'>)
Array(logical size: 5, items: [6, 7, 8, 9, 10], physical size: 8, data type: <class 'int'>)
Array(logical size: 6, items: [5, 6, 7, 8, 9, 10], physical size: 8, data type: <class 'int'>)
Array(logical size: 7, items: [4, 5, 6, 7, 8, 9, 10], physical size: 8, data type: <class 'int'>)
Array(logical size: 8, items: [3, 4, 5, 6, 7, 8, 9, 10], physical size: 8, data type: <class 'int'>)
Array(logical size: 9, items: [2, 3, 4, 5, 6, 7, 8, 9, 10], physical size: 16, data type: <class 'int'>)
Array(logical size: 10, items: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], physical size: 16, data type: <class 'int'>)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
T
|
the desired data to append. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/datastructures/iarray.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
clear()
abstractmethod
Clear the Array
Examples:
>>> array = Array[int](starting_sequence=[num for num in range(10)], data_type=int)
>>> print(repr(array))
Array(logical size: 10, items: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], physical size: 10, data type: <class 'int'>)
>>> array.clear()
>>> print(repr(array))
Array(logical size: 0, items: [], physical size: 0, data type: <class 'int'>)
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/datastructures/iarray.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
pop()
abstractmethod
Pop an item from the end of the Array. Internally, the Array should manage a physical size (the size of the internal numpy array) and a logical size (the number of items in the Array). The algorithm should shrink the array physical size by half when the logical size is less than or equal to 1/4 of the physical size.
Examples:
>>> array = Array[int](starting_sequence=[num for num in range(10)], data_type=int)
>>> print(repr(array))
Array(logical size: 9 items: [0, 1, 2, 3, 4, 5, 6, 7, 8] physical size: 10, data type: <class 'int'>)
>>> for i in range(10):
... array.pop()
... print(repr(array))
Array(logical size: 8 items: [0, 1, 2, 3, 4, 5, 6, 7] physical size: 10, data type: <class 'int'>)
Array(logical size: 7 items: [0, 1, 2, 3, 4, 5, 6] physical size: 10, data type: <class 'int'>)
Array(logical size: 6 items: [0, 1, 2, 3, 4, 5] physical size: 10, data type: <class 'int'>)
Array(logical size: 5 items: [0, 1, 2, 3, 4] physical size: 10, data type: <class 'int'>)
Array(logical size: 4 items: [0, 1, 2, 3] physical size: 10, data type: <class 'int'>)
Array(logical size: 3 items: [0, 1, 2] physical size: 10, data type: <class 'int'>)
Array(logical size: 2 items: [0, 1] physical size: 5, data type: <class 'int'>)
Array(logical size: 1 items: [0] physical size: 2, data type: <class 'int'>)
Array(logical size: 0 items: [] physical size: 1, data type: <class 'int'>)
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/datastructures/iarray.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
pop_front()
abstractmethod
Pop an item from the front of the Array. Internally, the Array should manage a physical size (the size of the internal numpy array) and a logical size (the number of items in the Array). The algorithm should shrink the array physical size by half when the logical size is less than or equal to 1/4 of the physical size.
Examples:
>>> array = Array[int](starting_squence[num for num in range(10)], data_type=int)
>>> print(repr(array))
Array(logical size: 9 items: [0, 1, 2, 3, 4, 5, 6, 7, 8] physical size: 10, data type: <class 'int'>)
>>> for i in range(10):
... array.pop_front()
... print(repr(array))
Array(logical size: 8 items: [1, 2, 3, 4, 5, 6, 7, 8] physical size: 10, data type: <class 'int'>)
Array(logical size: 7 items: [2, 3, 4, 5, 6, 7, 8] physical size: 10, data type: <class 'int'>)
Array(logical size: 6 items: [3, 4, 5, 6, 7, 8] physical size: 10, data type: <class 'int'>)
Array(logical size: 5 items: [4, 5, 6, 7, 8] physical size: 10, data type: <class 'int'>)
Array(logical size: 4 items: [5, 6, 7, 8] physical size: 10, data type: <class 'int'>)
Array(logical size: 3 items: [6, 7, 8] physical size: 10, data type: <class 'int'>)
Array(logical size: 2 items: [7, 8] physical size: 5, data type: <class 'int'>)
Array(logical size: 1 items: [8] physical size: 2, data type: <class 'int'>)
Array(logical size: 0 items: [] physical size: 1, data type: <class
Returns: None
Raises:
| Type | Description |
|---|---|
IndexError
|
if the Array is empty. |
Source code in src/datastructures/iarray.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |