mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-25 19:19:20 -04:00
Add function to flatten boggle board results into 1D list
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""Boggler Demo"""
|
||||
import sys
|
||||
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 len(sys.argv) < 3 or len(sys.argv) > 4:
|
||||
@@ -23,6 +23,10 @@ if __name__ == '__main__':
|
||||
print("\nBOARD")
|
||||
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():
|
||||
print(f"\nStarting @ {start_pos}...")
|
||||
for word in tree.word_paths:
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
from __future__ import annotations
|
||||
from os import path
|
||||
from multiprocessing import Pool
|
||||
import functools
|
||||
import operator
|
||||
|
||||
class BoardCell:
|
||||
'''Boggle Board cell'''
|
||||
@@ -418,3 +420,10 @@ def read_boggle_file(file):
|
||||
'''Return list of rows from Boggle board csv file'''
|
||||
with open(file, 'r', encoding='utf-8') as file:
|
||||
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
|
||||
Reference in New Issue
Block a user