-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmy2darray.py
36 lines (31 loc) · 1.15 KB
/
my2darray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from myarray import Array
class Array2D:
def __init__(self, numRows, numCols):
self._theRows = Array(numRows)
for i in range(numRows):
self._theRows[i] = Array(numCols)
def numRows(self):
return len(self._theRows)
def numCols(self):
return len(self._theRows[0])
def clear(self, value):
for row in range(self.numRows()):
row.clear(value)
def __getitem__(self, ndxTuple):
assert len(ndxTuple)==2, "Invalid number of array subscripts"
row = ndxTuple[0]
col = ndxTuple[1]
assert row >= 0 and row < self.numRows() \
and col >= 0 and col < self.numCols(),\
"Array subscript out of range"
the1dArray = self._theRows[row]
return the1dArray[col]
def __setitem__(self, ndxTuple, value):
assert len(ndxTuple)==2, "Invalid no of array subscripts"
row = ndxTuple[0]
col = ndxTuple[1]
assert row >=0 and row < self.numRows() \
and col >= 0 and col < self.numCols(), \
"Array subscript out of range."
the1dArray = self._theRows[row]
the1dArray[col] = value