Module dalpy.arrays
Module that holds classes and functions related to 1 and 2 dimensional arrays.
This module contains the Array
and Array2D
classes and the sort()
function. Array
represents a 1D array. Array2D
represents a 2D array. sort()
can be used to return a sorted copy of an Array
.
Examples
Creating an Array
, setting its elements, and getting a sorted copy:
a = Array(3)
a[0] = 1
a[1] = 4
a[2] = 2
b = sort(a)
Creating an Array2D
and setting its elements:
c = Array2D(3,2)
for i in range(3):
for j in range(2):
c[i,j] = i + j
Functions
def sort(a)
Classes
class Array (length)
-
Represents a 1D array.
This class represents a 1D fixed length array. It should be used like a Java array. That is, you cannot increase its length, slice it, or index it with negative numbers like a Python
list
. One cannot iterate over anArray
like a Pythonlist
either. For example,for e in a
will raise aRuntimeError
for anArray
a
. A user should assume that indexing into anArray
(either for getting or setting elements) is done inO(1)
time with respect to the length of theArray
.Examples
After constructing an
Array
, one can get an element at a particular index using square brackets[]
as follows:a = Array(2) b = a[0]
Here,
b
will store the first element ina
.Similarly, one can set an element of an
Array
using a combination of[]
and the assignment operator=
a = Array(2) a[0] = 1
Here, the first element of
a
is set to 1.Note that an
IndexError
will be raised in the event one supplies an index in[]
that is negative or greater than or equal to the length of theArray
.Initializes an
Array
to be a particular length.Each of the length elements of this
Array
will be set toNone
. This runs inO(length)
time.Args
length
- An integer specifying the length of the
Array
.
Raises
ValueError
- If
length < 0
.
Methods
def length(self)
def sort(self)
-
Sorts the elements of this
Array
in place.Note that this modifies this
Array
, it does not return a sorted copy. The user must make sure that the elements of theArray
can be compared. For example, an emptyArray
cannot be sorted asNoneType
(None
) elements cannot be compared. This method runs inO(n * log(n))
time wheren
is the length of theArray
.
class Array2D (num_rows, num_columns)
-
Represents a 2D Array.
This class represents a 2D array with a fixed number of rows and columns. It should be used like a Java two-dimensional array. That is, you cannot increase its dimensions, slice it, or index it with negative numbers like a Python list. Indexing into an
Array2D
(either for getting or setting) is done inO(1)
time with respect to the dimensions of theArray2D
.Examples
After constructing an
Array2D
, one can get an element at a particular (row, column) index using square brackets[]
as follows:a = Array2D(2,2) b = a[0,1]
Here,
b
will store the element in the second column of the first row ofa
.Similarly, one can set an element of an
Array2D
using a combination of[]
and the assignment operator=
a = Array2D(2,2) a[0,1] = 2
Here, the element in the second column of the first of
a
is set to 2.Note that an
IndexError
will be raised in the event one supplies an index in[]
that (i) has a row that is negative or greater than or equal to the number of rows of theArray2D
or (ii) a column that is negative or greater than or equal to the number of columns of theArray2D
.Initializes an
Array2D
to be a particular number of rows and columns.Each of the
num_rows * num_columns
elements of thisArray
will be set toNone
. This method runs inO(num_rows * num_columns)
time.Args
num_rows
- An integer specifying the number of rows this
Array2D
should have. num_columns
- An integer specifying the number of columns each of the rows of this
Array2D
should have.
Raises
ValueError
- If
num_rows <= 0
ornum_columns <= 0
.
Methods
def columns(self)
def rows(self)