From a1b50fad67fdf15ad1363c21ff444f13077dcd8d Mon Sep 17 00:00:00 2001 From: Jason Varbedian Date: Tue, 10 Sep 2024 14:31:36 -0700 Subject: [PATCH 1/2] What i submitted --- python/practice.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/python/practice.py b/python/practice.py index b5d2c68..c0c8cc3 100644 --- a/python/practice.py +++ b/python/practice.py @@ -2341,6 +2341,62 @@ def getIntersectionNode(headA: ListNode, headB: ListNode) -> Optional[ListNode]: return pA +def max_words_packed(board, dictionary): + directions = [(0,1), (1,0), (0, -1), (-1, 0)] #right left up down + def dfs(used, words_packed): + nonlocal max_packed + max_packed = max(max_packed, len(words_packed)) + print(f"words {words_packed}") + for word in dictionary: + if word in words_packed: + continue + for i in range(len(board)): + for j in range(len(board[0])): + if can_place_word(board, word, i ,j, used, set()): + new_used = used | set((i, j) for i, j in word_positions) + dfs(new_used, words_packed + [word]) + + # check if its fits + # see if you can make your way through the board + def can_place_word(board, word, i, j, used, current_path): + # track path + if len(current_path) == len(word): + global word_positions + word_positions = current_path + return True + # all that work + if (i < 0 or i>= len(board) or j < 0 or j >= len(board[0]) or + (i,j) in used or (i, j) in current_path or + board[i][j] != word[len(current_path)]): + return False + if len(used) > 0 or len(current_path) > 0: + print(f"used {used} path{current_path}") + + current_path.add((i, j)) + for di, dj in directions: + if can_place_word(board, word, i + di, j+ dj, used, current_path): + return True + current_path.remove((i, j)) + return False + + max_packed = 0 + word_positions= set() + dfs(set(), []) + return max_packed + +def test_max_words_packed(): + board = [ + ['E', 'N', 'P'], + ['G', 'S', 'C'], + ['O', 'N', 'S'] + ] + dictionary = ['EGO', 'EGOS', 'EGONS', 'SNO', 'SONGS', 'NC', 'SNS', 'P'] + result = max_words_packed(board, dictionary) + print(result) + assert result == 3 + + + # python practice.py if __name__ == "__main__": test_FileStore() @@ -2411,5 +2467,7 @@ def getIntersectionNode(headA: ListNode, headB: ListNode) -> Optional[ListNode]: test_sumOfUnique() test_longestCommonPrefix() + test_max_words_packed() + # concurency print("All tests passed") From af72b52dc8ff9b5fe0795f704e3210afabd13410 Mon Sep 17 00:00:00 2001 From: Jason Varbedian Date: Tue, 10 Sep 2024 14:34:27 -0700 Subject: [PATCH 2/2] changes --- python/practice.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/practice.py b/python/practice.py index c0c8cc3..b1d55f9 100644 --- a/python/practice.py +++ b/python/practice.py @@ -2345,6 +2345,7 @@ def max_words_packed(board, dictionary): directions = [(0,1), (1,0), (0, -1), (-1, 0)] #right left up down def dfs(used, words_packed): nonlocal max_packed + nonlocal word_positions max_packed = max(max_packed, len(words_packed)) print(f"words {words_packed}") for word in dictionary: @@ -2359,9 +2360,9 @@ def dfs(used, words_packed): # check if its fits # see if you can make your way through the board def can_place_word(board, word, i, j, used, current_path): + nonlocal word_positions # track path if len(current_path) == len(word): - global word_positions word_positions = current_path return True # all that work @@ -2370,7 +2371,7 @@ def can_place_word(board, word, i, j, used, current_path): board[i][j] != word[len(current_path)]): return False if len(used) > 0 or len(current_path) > 0: - print(f"used {used} path{current_path}") + print(f"used {used} path{current_path} word_positions{word_positions}") current_path.add((i, j)) for di, dj in directions: