Skip to content

Array

Download Source Files

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
class IArray2D(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
    """
    class IRow(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.
        """
        @abstractmethod
        def __init__(self, row_index: int, array: IArray, num_columns: int) -> None:
            """ Initializes the Row object. 

            Args:
                row_index (int): The index of the row in the two-dimensional array.
                array (IArray): The two-dimensional array that the row belongs to.
                num_columns (int): The number of columns in the row.
            """
            ... 

        @abstractmethod
        def __getitem__(self, column_index: int) -> T:
            """ 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. """
            ...

        @abstractmethod
        def __setitem__(self, column_index: int, value: T) -> None:
            """ 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. """
            ...

        @abstractmethod
        def __iter__(self) -> Iterator[T]:
            """ Returns an iterator of the row. 

            Returns:
                Iterator[T]: An iterator of the row.
            """
            ...

        @abstractmethod
        def __reversed__(self) -> Iterator[T]:
            """ Returns a reverse iterator of the row. 

            Returns:
                Iterator[T]: A reverse iterator of the row.
            """
            ...

        @abstractmethod
        def __len__(self) -> int:
            """ Returns the number of columns in the row.

            Returns:
                int: The number of columns in the row.
            """
            ...

    @abstractmethod
    def __init__(self, starting_sequence: Sequence[Sequence[T]]=[[]], data_type=object) -> None: 
        """ 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]]
            >>> 

        Args:
            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).

        Raises:
            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:
            None            
            """
        ...

    @staticmethod
    @abstractmethod
    def empty(rows: int=0, cols: int=0, data_type: type=object) -> IArray2D[T]: 
        """ Creates an empty two-dimensional array with the specified number of rows and columns.

        Args:
            rows (int): The number of rows in the two-dimensional array (default: 0).
            cols (int): The number of columns in the two-dimensional array (default: 0).
            data_type (type): The type of data that the array will hold (default: object).

        Returns:
            IArray2D[T]: An empty two-dimensional array of the specified size and data type.
        """
        ...

    @abstractmethod
    def __getitem__(self, index: int) -> IRow[T]: 
        """ 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:
            IArray[T] | IArray2D[T]: The item at the specified index or a slice of the two-dimensional array.

        Raises:
            IndexError: If the index is out of bounds.

        """
        ...

    @abstractmethod
    def __iter__(self) -> Iterator[Sequence[T]]:
        """ 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:
            Iterator[IArray[T]]: An iterator of the two-dimensional array.
        """
        ...

    @abstractmethod
    def __reversed__(self) -> Iterator[Sequence[T]]:
        """ Returns an iterator of the two"""

    @abstractmethod
    def __len__(self) -> int:
        """ Returns the number of rows in the two-dimensional array.

        Returns:
            int: The number of rows in the two-dimensional array.
        """

    @abstractmethod
    def __str__(self) -> str:
        """ 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:
            str: A string representation of the two-dimensional array.
        """
        ...

    @abstractmethod
    def __repr__(self) -> str:
        """ 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:
            str: A string representation of the two-dimensional array.
        """
        ...

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
class IRow(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.
    """
    @abstractmethod
    def __init__(self, row_index: int, array: IArray, num_columns: int) -> None:
        """ Initializes the Row object. 

        Args:
            row_index (int): The index of the row in the two-dimensional array.
            array (IArray): The two-dimensional array that the row belongs to.
            num_columns (int): The number of columns in the row.
        """
        ... 

    @abstractmethod
    def __getitem__(self, column_index: int) -> T:
        """ 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. """
        ...

    @abstractmethod
    def __setitem__(self, column_index: int, value: T) -> None:
        """ 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. """
        ...

    @abstractmethod
    def __iter__(self) -> Iterator[T]:
        """ Returns an iterator of the row. 

        Returns:
            Iterator[T]: An iterator of the row.
        """
        ...

    @abstractmethod
    def __reversed__(self) -> Iterator[T]:
        """ Returns a reverse iterator of the row. 

        Returns:
            Iterator[T]: A reverse iterator of the row.
        """
        ...

    @abstractmethod
    def __len__(self) -> int:
        """ Returns the number of columns in the row.

        Returns:
            int: The number of columns in the row.
        """
        ...

__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
@abstractmethod
def __getitem__(self, column_index: int) -> T:
    """ 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. """
    ...

__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
@abstractmethod
def __init__(self, row_index: int, array: IArray, num_columns: int) -> None:
    """ Initializes the Row object. 

    Args:
        row_index (int): The index of the row in the two-dimensional array.
        array (IArray): The two-dimensional array that the row belongs to.
        num_columns (int): The number of columns in the row.
    """
    ... 

__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
@abstractmethod
def __iter__(self) -> Iterator[T]:
    """ Returns an iterator of the row. 

    Returns:
        Iterator[T]: An iterator of the row.
    """
    ...

__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
@abstractmethod
def __len__(self) -> int:
    """ Returns the number of columns in the row.

    Returns:
        int: The number of columns in the row.
    """
    ...

__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
@abstractmethod
def __reversed__(self) -> Iterator[T]:
    """ Returns a reverse iterator of the row. 

    Returns:
        Iterator[T]: A reverse iterator of the row.
    """
    ...

__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
@abstractmethod
def __setitem__(self, column_index: int, value: T) -> None:
    """ 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. """
    ...

__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
@abstractmethod
def __getitem__(self, index: int) -> IRow[T]: 
    """ 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:
        IArray[T] | IArray2D[T]: The item at the specified index or a slice of the two-dimensional array.

    Raises:
        IndexError: If the index is out of bounds.

    """
    ...

__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
@abstractmethod
def __init__(self, starting_sequence: Sequence[Sequence[T]]=[[]], data_type=object) -> None: 
    """ 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]]
        >>> 

    Args:
        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).

    Raises:
        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:
        None            
        """
    ...

__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
@abstractmethod
def __iter__(self) -> Iterator[Sequence[T]]:
    """ 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:
        Iterator[IArray[T]]: An iterator of the two-dimensional array.
    """
    ...

__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
@abstractmethod
def __len__(self) -> int:
    """ Returns the number of rows in the two-dimensional array.

    Returns:
        int: The number of rows in the two-dimensional array.
    """

__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
@abstractmethod
def __repr__(self) -> str:
    """ 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:
        str: A string representation of the two-dimensional array.
    """
    ...

__reversed__() abstractmethod

Returns an iterator of the two

Source code in src/datastructures/iarray2d.py
145
146
147
@abstractmethod
def __reversed__(self) -> Iterator[Sequence[T]]:
    """ Returns an iterator of the two"""

__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
@abstractmethod
def __str__(self) -> str:
    """ 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:
        str: A string representation of the two-dimensional array.
    """
    ...

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
@staticmethod
@abstractmethod
def empty(rows: int=0, cols: int=0, data_type: type=object) -> IArray2D[T]: 
    """ Creates an empty two-dimensional array with the specified number of rows and columns.

    Args:
        rows (int): The number of rows in the two-dimensional array (default: 0).
        cols (int): The number of columns in the two-dimensional array (default: 0).
        data_type (type): The type of data that the array will hold (default: object).

    Returns:
        IArray2D[T]: An empty two-dimensional array of the specified size and data type.
    """
    ...