-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray2D.py
52 lines (43 loc) · 1.65 KB
/
Array2D.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from Array 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()):
self._theRows[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
# 2D Array Implementation
# twoDArray = Array2D(3,3)
# twoDArray.clear(0)
# for i in range(twoDArray.numRows()):
# print(" ")
# for j in range(twoDArray.numCols()):
# twoDArray[i,j] = int(input("Enter Value %d %d = " %(i,j)))
# print("Your 2D-Array is here: ")
# for i in range(twoDArray.numRows()):
# print(" ")
# for j in range(twoDArray.numCols()):
# print(twoDArray[i,j], end = " ")