I need to create a ragged two dimensional grid in Python which has 3 rows where the 1st row has 3 columns, 2nd row - 6 columns and 3rd row - 9 columns. All that should be done without using any packages like NumPy or anything else, only with classes and methods like in the following example. Here is an example how I create regular 2d array
class newArray ():
def __init__(self, capacity, fill = None):
self.item = list()
for count in range (capacity):
self.item.append(fill)
def __str__(self):
return str (self.item)
def __len__(self):
return len(self.item)
def __iter__(self):
return iter(self.item)
def __getitem__(self, index):
return self.item[index]
def __setitem__(self, index, newItem):
self.item[index] = newItem
class raggedGrid():
def __init__(self, rows, cols):
self.data = newArray(rows)
for row in range(rows):
self.data[row] = newArray(cols)
def getRows(self):
return len(self.data)
def getCols(self):
return len(self.data[0])
def __getitem__(self, index):
return self.data[index]
def __setitem__(self, index, newItem):
self.data[index] = newItem
def __str__(self):
result = '\n'
for row in range(self.getRows()):
for col in range(self.getCols()):
result += str(self.data[row][col]) + ' '
result += '\n'
return result
After declaring it prints out a grid
a = raggedGrid(4,4)
print(a)
None None None None
None None None None
None None None None
None None None None
I stuck and don't know from where to start with this
Here is modified version of
raggedGrid
class:Here's an example usage for that: