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 Array2D interface that represents a two-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 Array2D class in the array2d.py file.
IArray2D
Bases: Generic[T], ABC
An interface that represents the minimal functions needed to make an Array object into a two-dimensional array. Other typical functions like str, len, repr, etc. are not included in this interface because they are provided by the IArray
Source code in src/datastructures/iarray2d.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 | |
IRow
Bases: Generic[T], ABC
An interface that represents a row in a two-dimensional array in order to support iteration and indexing. This class is not intended to be used directly by a user, but rather as a helper class for the Array2D to provide the second bracket operator.
Source code in src/datastructures/iarray2d.py
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 | |
__getitem__(column_index)
abstractmethod
Gets the item at the specified column index in the row. This method is intended to be used by the Array2D class to provide the second bracket operator.
Source code in src/datastructures/iarray2d.py
39 40 41 42 43 | |
__init__(row_index, array, num_columns)
abstractmethod
Initializes the Row object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row_index
|
int
|
The index of the row in the two-dimensional array. |
required |
array
|
IArray
|
The two-dimensional array that the row belongs to. |
required |
num_columns
|
int
|
The number of columns in the row. |
required |
Source code in src/datastructures/iarray2d.py
28 29 30 31 32 33 34 35 36 37 | |
__iter__()
abstractmethod
Returns an iterator of the row.
Returns:
| Type | Description |
|---|---|
Iterator[T]
|
Iterator[T]: An iterator of the row. |
Source code in src/datastructures/iarray2d.py
51 52 53 54 55 56 57 58 | |
__len__()
abstractmethod
Returns the number of columns in the row.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of columns in the row. |
Source code in src/datastructures/iarray2d.py
69 70 71 72 73 74 75 76 | |
__reversed__()
abstractmethod
Returns a reverse iterator of the row.
Returns:
| Type | Description |
|---|---|
Iterator[T]
|
Iterator[T]: A reverse iterator of the row. |
Source code in src/datastructures/iarray2d.py
60 61 62 63 64 65 66 67 | |
__setitem__(column_index, value)
abstractmethod
Sets the item at the specified column index in the row. This method is intended to be used by the Array2D class to provide the second bracket operator.
Source code in src/datastructures/iarray2d.py
45 46 47 48 49 | |
__getitem__(index)
abstractmethod
Gets the item at the specified index or a slice of the two-dimensional array. If the index is a slice, the method should return an IArray2D with the specified slice of the two-dimensional array. Otherwise, it should return an array at the specified index since the elements of the two-dimensional array are arrays. Args: index (int | slice): The index or slice of the two-dimensional array.
Returns:
| Type | Description |
|---|---|
IRow[T]
|
IArray[T] | IArray2D[T]: The item at the specified index or a slice of the two-dimensional array. |
Raises:
| Type | Description |
|---|---|
IndexError
|
If the index is out of bounds. |
Source code in src/datastructures/iarray2d.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
__init__(starting_sequence=[[]], data_type=object)
abstractmethod
Initializes the Array2D object with a starting sequence of sequences.
Examples:
>>> array2d = Array2D([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> repr(array2d)
Array2D [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>>
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
starting_sequence
|
Sequence[Sequence[T]]
|
The starting sequence of sequences (default: [[]]). |
[[]]
|
data_type
|
type
|
The type of data that the array will hold (default: object). |
object
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the starting sequence is not a valid sequence. |
ValueError
|
If the starting sequence is not a sequence of sequences. |
TypeError
|
If all items are not of the same type. |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in src/datastructures/iarray2d.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
__iter__()
abstractmethod
Returns an iterator of the two-dimensional array. Because the two dimensional array is an IArray, this method should return an iterator to the IArray.
Returns:
| Type | Description |
|---|---|
Iterator[Sequence[T]]
|
Iterator[IArray[T]]: An iterator of the two-dimensional array. |
Source code in src/datastructures/iarray2d.py
135 136 137 138 139 140 141 142 143 | |
__len__()
abstractmethod
Returns the number of rows in the two-dimensional array.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of rows in the two-dimensional array. |
Source code in src/datastructures/iarray2d.py
149 150 151 152 153 154 155 | |
__repr__()
abstractmethod
Returns a string representation of the two-dimensional array. The string representation should include the string representation of each array in the two-dimensional array.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A string representation of the two-dimensional array. |
Source code in src/datastructures/iarray2d.py
167 168 169 170 171 172 173 174 175 | |
__reversed__()
abstractmethod
Returns an iterator of the two
Source code in src/datastructures/iarray2d.py
145 146 147 | |
__str__()
abstractmethod
Returns a string representation of the two-dimensional array. The string representation should include the string representation of each array in the two-dimensional array.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A string representation of the two-dimensional array. |
Source code in src/datastructures/iarray2d.py
157 158 159 160 161 162 163 164 165 | |
empty(rows=0, cols=0, data_type=object)
abstractmethod
staticmethod
Creates an empty two-dimensional array with the specified number of rows and columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
int
|
The number of rows in the two-dimensional array (default: 0). |
0
|
cols
|
int
|
The number of columns in the two-dimensional array (default: 0). |
0
|
data_type
|
type
|
The type of data that the array will hold (default: object). |
object
|
Returns:
| Type | Description |
|---|---|
IArray2D[T]
|
IArray2D[T]: An empty two-dimensional array of the specified size and data type. |
Source code in src/datastructures/iarray2d.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |