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 anArraylike a Pythonlisteither. For example,for e in awill raise aRuntimeErrorfor anArraya. 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,
bwill store the first element ina.Similarly, one can set an element of an
Arrayusing a combination of[]and the assignment operator=a = Array(2) a[0] = 1Here, the first element of
ais set to 1.Note that an
IndexErrorwill 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
Arrayto be a particular length.Each of the length elements of this
Arraywill 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
Arrayin place.Note that this modifies this
Array, it does not return a sorted copy. The user must make sure that the elements of theArraycan be compared. For example, an emptyArraycannot be sorted asNoneType(None) elements cannot be compared. This method runs inO(n * log(n))time wherenis 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,
bwill store the element in the second column of the first row ofa.Similarly, one can set an element of an
Array2Dusing a combination of[]and the assignment operator=a = Array2D(2,2) a[0,1] = 2Here, the element in the second column of the first of
ais set to 2.Note that an
IndexErrorwill 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 theArray2Dor (ii) a column that is negative or greater than or equal to the number of columns of theArray2D.Initializes an
Array2Dto be a particular number of rows and columns.Each of the
num_rows * num_columnselements of thisArraywill be set toNone. This method runs inO(num_rows * num_columns)time.Args
num_rows- An integer specifying the number of rows this
Array2Dshould have. num_columns- An integer specifying the number of columns each of the rows of this
Array2Dshould have.
Raises
ValueError- If
num_rows <= 0ornum_columns <= 0.
Methods
def columns(self)def rows(self)