-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgrid.py
More file actions
32 lines (25 loc) · 915 Bytes
/
grid.py
File metadata and controls
32 lines (25 loc) · 915 Bytes
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
from arrays import Array
class Grid(object):
"""Represents a two-dimensional array."""
def __init__(self, rows, columns, fillValue=None):
self._data = Array(rows)
for row in range(rows):
self._data[row] = Array(columns, fillValue)
def getHeight(self):
"""Return the number of rows."""
return len(self._data)
def getWidth(self):
"""Return the number of columns."""
return len(self._data[0])
def __getitem__(self, index):
"""Supports two-dimensional indexing
with [row][column]."""
return self._data[index]
def __str__(self):
"""Return a string representation of the grid."""
result = ""
for row in range(self.getHeight()):
for col in range(self.getWidth()):
result += str(self._data[row][col]) + " "
result += "\n"
return result