0

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

Dima
  • 3
  • 2

1 Answers1

0

Here is modified version of raggedGrid class:

class raggedGrid():
    
    def __init__(self, rows, *col_lengths):
        self.data = newArray(rows)
        for row in range(rows):
            self.data[row] = newArray(col_lengths[row])
        
                    
    def getRows(self):
        return len(self.data)
    
    def getCols(self, row_index=None):
        if row_index is None:
            return [len(self.data[row]) for row in self.data]
        else:
            return len(self.data[row_index])
    
    
    def __getitem__(self, index):
        return self.data[index]
    
    def __setitem__(self, index, newItem):
        self.data[index] = newItem
    
    def __str__(self):
        result = '\n'
        for row_index in range(self.getRows()):
            for col_index in range(self.getCols(row_index)):
                result += repr(self.data[row_index][col_index]) + ' '
            result += '\n'
        return result

Here's an example usage for that:

a = raggedGrid(4,2,7,16,3) # Four row, having respective lengths of 2,7,16,3
print(a)
a[1][4] = "Val at [1][4]"
a[2][13] = "Val at [2][13]"
print ("Printing a after some elements were assigned")
print (a)
fountainhead
  • 3,584
  • 1
  • 8
  • 17
  • Thanks a lot. I got it – Dima Nov 25 '20 at 14:25
  • Just want to clarify what * means near col_lengths argument? When I delete it I get a mistake ''__init__() takes 3 positional arguments but 5 were given''. – Dima Nov 25 '20 at 14:35
  • It means that, at the place where we are invoking that function, zero or more arguments are allowed to appear in the arg-list at that position, rather than just one argument. Here, we want to pass several col-lengths, and not just one col-length, at the time of invoking that function. – fountainhead Nov 25 '20 at 14:38
  • One further simplification of `__init__()` can be as follows: Instead of `def __init(self, rows, *col_lengths):`, just have `def __init(self, *col_lengths):`. Then,, inside the body of `__init__()`, you should get the value of `rows` as `rows = len(col_lengths)`. This means, at the place where the function is called, we don't have to pass the value of `rows`. We just pass the comma-separated col-lengths (which we are already passing now). If we pass five such col-lengths, it means that the value of `rows` is `5`. – fountainhead Nov 25 '20 at 16:15