From 9cdf9292a31b6334a2467e2c32116600d75df34b Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 4 Mar 2023 03:30:39 +0300 Subject: [PATCH 1/3] Reduce the complexity of backtracking/word_search.py --- backtracking/word_search.py | 110 +++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/backtracking/word_search.py b/backtracking/word_search.py index 25d1436be36e..002b3398f17f 100644 --- a/backtracking/word_search.py +++ b/backtracking/word_search.py @@ -33,6 +33,59 @@ """ +# Returns the hash key of matrix indexes. +def get_point_key(len_board: int, len_board_column: int, row: int, column: int) -> int: + """ + >>> get_point_key(10, 20, 1, 0) + 200 + """ + + return len_board * len_board_column * row + column + + +# Return True if it's possible to search the word suffix +# starting from the word_index. +def exits_word( + board: list[list[str]], + word: str, + row: int, + column: int, + word_index: int, + visited_points_set: set[int], +) -> bool: + """ + >>> exits_word([["A"]], "B", 0, 0, 0, set()) + False + """ + + if board[row][column] != word[word_index]: + return False + + if word_index == len(word) - 1: + return True + + traverts_directions = [(0, 1), (0, -1), (-1, 0), (1, 0)] + len_board = len(board) + len_board_column = len(board[0]) + for direction in traverts_directions: + next_i = row + direction[0] + next_j = column + direction[1] + if not (0 <= next_i < len_board and 0 <= next_j < len_board_column): + continue + + key = get_point_key(len_board, len_board_column, next_i, next_j) + if key in visited_points_set: + continue + + visited_points_set.add(key) + if exits_word(board, word, next_i, next_j, word_index + 1, visited_points_set): + return True + + visited_points_set.remove(key) + + return False + + def word_exists(board: list[list[str]], word: str) -> bool: """ >>> word_exists([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED") @@ -77,6 +130,8 @@ def word_exists(board: list[list[str]], word: str) -> bool: board_error_message = ( "The board should be a non empty matrix of single chars strings." ) + + len_board = len(board) if not isinstance(board, list) or len(board) == 0: raise ValueError(board_error_message) @@ -94,61 +149,12 @@ def word_exists(board: list[list[str]], word: str) -> bool: "The word parameter should be a string of length greater than 0." ) - traverts_directions = [(0, 1), (0, -1), (-1, 0), (1, 0)] - len_word = len(word) - len_board = len(board) len_board_column = len(board[0]) - - # Returns the hash key of matrix indexes. - def get_point_key(row: int, column: int) -> int: - """ - >>> len_board=10 - >>> len_board_column=20 - >>> get_point_key(0, 0) - 200 - """ - - return len_board * len_board_column * row + column - - # Return True if it's possible to search the word suffix - # starting from the word_index. - def exits_word( - row: int, column: int, word_index: int, visited_points_set: set[int] - ) -> bool: - """ - >>> board=[["A"]] - >>> word="B" - >>> exits_word(0, 0, 0, set()) - False - """ - - if board[row][column] != word[word_index]: - return False - - if word_index == len_word - 1: - return True - - for direction in traverts_directions: - next_i = row + direction[0] - next_j = column + direction[1] - if not (0 <= next_i < len_board and 0 <= next_j < len_board_column): - continue - - key = get_point_key(next_i, next_j) - if key in visited_points_set: - continue - - visited_points_set.add(key) - if exits_word(next_i, next_j, word_index + 1, visited_points_set): - return True - - visited_points_set.remove(key) - - return False - for i in range(len_board): for j in range(len_board_column): - if exits_word(i, j, 0, {get_point_key(i, j)}): + if exits_word( + board, word, i, j, 0, {get_point_key(len_board, len_board_column, i, j)} + ): return True return False From 5724cc16679aae2799b2c9ed2ee7f8d8275526aa Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sat, 4 Mar 2023 03:33:20 +0300 Subject: [PATCH 2/3] Lower the --max-complexity threshold in the file .flake8 --- .flake8 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.flake8 b/.flake8 index b68ee8533a61..77ca7a328a77 100644 --- a/.flake8 +++ b/.flake8 @@ -1,7 +1,7 @@ [flake8] max-line-length = 88 # max-complexity should be 10 -max-complexity = 19 +max-complexity = 17 extend-ignore = # Formatting style for `black` # E203 is whitespace before ':' From 9ebf420b475c7bbda4f0659fdeed95f450142192 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Fri, 10 Mar 2023 11:38:28 +0300 Subject: [PATCH 3/3] Fix review issues --- .flake8 | 2 +- backtracking/word_search.py | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.flake8 b/.flake8 index 77ca7a328a77..b68ee8533a61 100644 --- a/.flake8 +++ b/.flake8 @@ -1,7 +1,7 @@ [flake8] max-line-length = 88 # max-complexity should be 10 -max-complexity = 17 +max-complexity = 19 extend-ignore = # Formatting style for `black` # E203 is whitespace before ':' diff --git a/backtracking/word_search.py b/backtracking/word_search.py index 002b3398f17f..c9d52012b42b 100644 --- a/backtracking/word_search.py +++ b/backtracking/word_search.py @@ -33,9 +33,10 @@ """ -# Returns the hash key of matrix indexes. def get_point_key(len_board: int, len_board_column: int, row: int, column: int) -> int: """ + Returns the hash key of matrix indexes. + >>> get_point_key(10, 20, 1, 0) 200 """ @@ -43,8 +44,6 @@ def get_point_key(len_board: int, len_board_column: int, row: int, column: int) return len_board * len_board_column * row + column -# Return True if it's possible to search the word suffix -# starting from the word_index. def exits_word( board: list[list[str]], word: str, @@ -54,6 +53,9 @@ def exits_word( visited_points_set: set[int], ) -> bool: """ + Return True if it's possible to search the word suffix + starting from the word_index. + >>> exits_word([["A"]], "B", 0, 0, 0, set()) False """