def test_single_array(self): singleArray = SingleArray() for i in range(30): singleArray.add(i) print(singleArray.get(15)) print(singleArray.size()) singleArray.insert('test', 15) print(singleArray.get(15)) print(singleArray.size()) print(singleArray.remove(15)) print(singleArray.get(15)) print(singleArray.size())
class MatrixArray(IArray): def __init__(self, rows_count: int = None, vector: int = None): self._array = SingleArray() self._vector = vector if vector else 10 self._size = 0 self._rows_size = 0 if rows_count is not None: for i in range(rows_count): self._array.add(VectorArray(self._vector)) self._rows_size += 1 def size(self): return self._size def rows_size(self): return self._rows_size def __resize_replace(self): if self.size() == self._array.size() * self._vector: self._array.add(VectorArray(self._vector)) self._rows_size += 1 def __resize_insert(self): if self._array.size() == 0 or self._array.get( self._array.size() - 1).size() == self._vector: self._array.add(VectorArray(self._vector)) self._rows_size += 1 def __shift_right(self, index: int = None): if index is not None: stop_row = int(index / self._vector) stop_col = int(index % 10) - 1 single_array_size = self._array.size() - 1 for i in range(single_array_size, stop_row - 1, -1): vector_array = self._array.get(i) vector_array_size = vector_array.size() col_index = stop_col if i == stop_row else 0 for j in range(vector_array_size - 1, col_index - 1, -1): element = vector_array.get(j) if j + 1 == vector_array_size: if i == single_array_size - 1: self._array.get(i + 1).insert(element, 0) else: self._array.get(i + 1).replace(element, 0) else: vector_array.replace(element, j + 1) def add(self, item): self.__resize_insert() self._array.get(int(self._size / self._vector)).add(item) self._size += 1 def insert(self, item, index: int): self.__resize_insert() self.__shift_right(index) self._array.get(int(index / self._vector)).replace( item, int(index % self._vector) - 1) self._size += 1 def replace(self, item, index: int): self.__resize_replace() self._array.get(int(index / self._vector)).replace( item, int(index % self._vector) - 1) self._size += 1 def replace_by_indexes(self, item, x: int, y: int): self.get_row(x).replace(item, y) self._size += 1 def remove(self, index: int): row_index = int(index / self._vector) col_index = int(index % self._vector) - 1 self._array.get(row_index).replace(None, col_index) if self._array.get(row_index).size() == 0: self._array.remove(row_index) self._size -= 1 def get(self, index: int, default: str = None): try: return self._array.get(int( index / self._vector)).get(int(index % self._vector) - 1) except IndexError: return default def get_row(self, row: int, default: str = None): try: return self._array.get(row) except IndexError: return default