# this file available at: https://arachnoid.com/files/python01/ import re from typing import List def read_dict(datafile : str) -> List[str]: with open(datafile) as f: database : List[str] = sorted(f.read().lower().split('\n')) print(f'Words in database: {len(database):,}') return database def format_print(results : List[str]) -> None: screen_width = 80 max_column_width = 0 for word in results: word_length = len(word) max_column_width = max(max_column_width,word_length) max_column_width +=1 data_length = len(results) print(f'Found {data_length:,} results:\n') columns = int(screen_width/max_column_width) # always print at least one row rows = int(data_length/columns)+1 for row in range(rows): for column in range(columns): index = row + column * rows if index < data_length: word = results[index] else: word = '' print(f'{word:{max_column_width}}',end='') print('') def word_search(database : List[str],query_str : str) -> None: print(f'Searching for "{query_str}" ...') query = re.compile(query_str,flags=re.IGNORECASE) results = list(filter(query.match,database)) format_print(results) def palindrome_search(database : List[str],length : int) -> None: print(f'Searching for palindromes with length >= {length} ...') results = [w for w in database if len(w) >= length and w == w[::-1]] format_print(results) # dictionary source: # https://github.com/dwyl/english-words/blob/master/words_alpha.txt # copy available at https://arachnoid.com/files/python01/dict def main(palindrome: bool = False): source = 'dict/words_alpha.txt' database = read_dict(source) if palindrome: length = 8 # find palindromes >= length palindrome_search(database,length) else: # search for query matches query = '.*ezy' word_search(database,query) if __name__ == "__main__": main(True)