Advertisement
1nikitas

Untitled

Mar 20th, 2022
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. class Table(object):
  2.  
  3.     def __init__(self, rows, cols):
  4.         self._rows = rows
  5.         self._cols = cols
  6.         self._table = [[0] * cols for _ in range(rows)]
  7.  
  8.     def get_value(self, row, col):
  9.         return (self._table[row][col] if 0 <= row < self._rows and 0 <= col < self._cols
  10.                 else None)
  11.  
  12.     def set_value(self, row, col, value):
  13.         self._table[row][col] = value
  14.  
  15.     def n_rows(self):
  16.         return self._rows
  17.  
  18.     def n_cols(self):
  19.         return self._cols
  20.  
  21.     def delete_row(self, row):
  22.         self._table.pop(row)
  23.         self._rows -= 1
  24.  
  25.     def delete_col(self, col):
  26.         for row in range(self._rows):
  27.             self._table[row].pop(col)
  28.         self._cols -= 1
  29.  
  30.     def add_row(self, row):
  31.         self._table.insert(row, [0] * self._cols)
  32.         self._rows += 1
  33.  
  34.     def add_col(self, col):
  35.         for row in range(self._rows):
  36.             self._table[row].insert(col, 0)
  37.         self._cols += 1
  38.  
  39. def main():
  40.     # Example 1
  41.     tab = Table(3, 5)
  42.     tab.set_value(0, 1, 10)
  43.     tab.set_value(1, 2, 20)
  44.     tab.set_value(2, 3, 30)
  45.     for i in range(tab.n_rows()):
  46.         for j in range(tab.n_cols()):
  47.             print(tab.get_value(i, j), end=' ')
  48.         print()
  49.     print()
  50.  
  51.     tab.add_row(1)
  52.  
  53.     for i in range(tab.n_rows()):
  54.         for j in range(tab.n_cols()):
  55.             print(tab.get_value(i, j), end=' ')
  56.         print()
  57.     print()
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement