From 6d06569996cdd8fec6d0e762e9f4e2405a9d169b Mon Sep 17 00:00:00 2001 From: Dmitry Afanasyev Date: Tue, 12 Oct 2021 02:43:23 +0300 Subject: [PATCH] blocks searcher added --- .gitignore | 2 + blocks.py | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 blocks.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf6e5d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +blocks_test.py \ No newline at end of file diff --git a/blocks.py b/blocks.py new file mode 100644 index 0000000..6293054 --- /dev/null +++ b/blocks.py @@ -0,0 +1,112 @@ +import copy +from random import randint + +LIST_HEIGHT = 8 +LIST_WIDTH = 7 + + +# List generator for check +def list_generator() -> None: + test_list = [[randint(0, 1) for _ in range(LIST_WIDTH)] for _ in range(LIST_HEIGHT)] + print('arr = [\n', end='') + for i in range(LIST_HEIGHT): + print(f'{test_list[i]},') + print(']') + +# list_generator() + + +arr = [ + # 0 1 2 3 4 5 6 + [1, 0, 1, 1, 0, 1, 1, ], # 0 + [1, 0, 0, 0, 0, 1, 0, ], # 1 + [1, 1, 1, 0, 1, 0, 0, ], # 2 + [0, 0, 1, 0, 1, 0, 0, ], # 3 + [1, 0, 1, 0, 1, 0, 1, ], # 4 + [1, 0, 1, 0, 1, 0, 1, ], # 5 + # 0 1 2 3 4 5 6 +] + + +class BlockSearcher: + def __init__(self, array: list): + self.array = array + self.array_copy = copy.deepcopy(array) + self.width = len(array[1]) + self.height = len(array) + self.block_number = 0 + + def exists(self, row: int, column: int) -> int: + if self.array[row][column] == 1: + for row_index in range(-1, 1): + columns = range(-1, 1) if row_index == 0 else range(-1, 2) + for column_index in columns: + current_row = row + row_index + current_column = column + column_index + if 0 <= current_row < self.height and 0 <= current_column < self.width: + element = self.array_copy[current_row][current_column] + if current_row == row and current_column == column: + continue + if element != 0: + return element + + # add number to the element if it exists + def search(self, row: int, column: int) -> None: + element = self.exists(row, column) + if self.array[row][column] == 1 and not element: + self.block_number += 1 + self.array_copy[row][column] = f'1{self.block_number}' + elif self.array[row][column] == 1 and element: + self.array_copy[row][column] = element + + # check each element if it the same as all elements around him + def touch(self, row: int, column: int) -> None: + if self.array[row][column] == 1: + element = self.array_copy[row][column] + for row_index in range(-1, 2): + for column_index in range(-1, 2): + current_row = row + row_index + current_column = column + column_index + if 0 <= current_row < self.height and 0 <= current_column < self.width: + temp_element = self.array_copy[current_row][current_column] + if current_row == row and current_column == column: + continue + if temp_element != 0 and temp_element != element: + self.changer(self.array_copy, element, temp_element) + + # filter doubled array + def filter(self) -> None: + for row in range(self.height): + for column in range(self.width): + self.search(row, column) + for row in range(self.height): + for column in range(self.width): + self.touch(row, column) + + # change doubled values + def changer(self, array: list, element: str, insert: str) -> list: + for row in range(self.height): + for column in range(self.width): + if array[row][column] == element: + array[row][column] = insert + return array + + # count unique items in doubled array + def blocks_count(self) -> int: + maximum = set() + for row in range(self.height): + for elem in self.array_copy[row]: + maximum.add(elem) + maximum.remove(0) + return len(maximum) + + # print both arrays for debug + def print(self) -> None: + for row in range(self.height): + print(self.array[row], self.array_copy[row]) + + +bs = BlockSearcher(arr) +bs.filter() +# bs.print() # use for DEBUG +print(f'Total blocks: {bs.blocks_count()}')