Add function to flatten boggle board results into 1D list

This commit is contained in:
2022-10-10 12:00:42 -04:00
parent f4accfe959
commit e219954ba6
2 changed files with 14 additions and 1 deletions
+5 -1
View File
@@ -1,7 +1,7 @@
"""Boggler Demo""" """Boggler Demo"""
import sys import sys
from pathlib import Path from pathlib import Path
from boggler_utils import BoggleBoard, build_full_boggle_tree, read_boggle_file from boggler_utils import BoggleBoard, build_full_boggle_tree, read_boggle_file, find_paths_by_word
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) < 3 or len(sys.argv) > 4: if len(sys.argv) < 3 or len(sys.argv) > 4:
@@ -23,6 +23,10 @@ if __name__ == '__main__':
print("\nBOARD") print("\nBOARD")
print(boggle_board) print(boggle_board)
# found_words = find_paths_by_word(board, Path(sys.argv[2]), 16)
# for word in found_words:
# print(word)
for start_pos, tree in boggle_tree.items(): for start_pos, tree in boggle_tree.items():
print(f"\nStarting @ {start_pos}...") print(f"\nStarting @ {start_pos}...")
for word in tree.word_paths: for word in tree.word_paths:
+9
View File
@@ -2,6 +2,8 @@
from __future__ import annotations from __future__ import annotations
from os import path from os import path
from multiprocessing import Pool from multiprocessing import Pool
import functools
import operator
class BoardCell: class BoardCell:
'''Boggle Board cell''' '''Boggle Board cell'''
@@ -418,3 +420,10 @@ def read_boggle_file(file):
'''Return list of rows from Boggle board csv file''' '''Return list of rows from Boggle board csv file'''
with open(file, 'r', encoding='utf-8') as file: with open(file, 'r', encoding='utf-8') as file:
return [x.rstrip().split(',') for x in file.readlines()] return [x.rstrip().split(',') for x in file.readlines()]
def find_paths_by_word(board_letters, dictionary_path, max_len):
'''Return list of paths by word'''
boggle_board = BoggleBoard(board_letters, max_len)
boggle_tree = build_full_boggle_tree(boggle_board, dictionary_path)
paths_by_word = functools.reduce(operator.iconcat, [x.word_paths for x in boggle_tree.values()], [])
return paths_by_word