-
Notifications
You must be signed in to change notification settings - Fork 1
/
play_wordgame
executable file
·43 lines (31 loc) · 1.42 KB
/
play_wordgame
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
"""Generate a letter matrix and find all the words in it"""
import argparse
from wordsearch.wordsearch import Matrix
def main():
"""Create a Matrix and searches it for words in a wordlist"""
parser = argparse.ArgumentParser(description='Create a new Wordsearch')
parser.add_argument('size', type=grid_size_type,
help="height and width of our wordsearch grid (min: 3)")
parser.add_argument('wordfile', type=argparse.FileType('r'),
help="file including words to search for")
parser_args = parser.parse_args()
new_matrix = Matrix(parser_args.size)
words_to_find = create_word_list_from_file(parser_args.wordfile, parser_args.size)
words_found = []
for word in words_to_find:
if word not in words_found and word in new_matrix:
words_found.append(word)
print("\n{}\n\n{}\n".format(new_matrix, " ".join(sorted(words_found))))
def grid_size_type(size):
"""Create a new argparse type to validate grid size is an integer greater than 2"""
size = int(size)
if size <= 2:
raise argparse.ArgumentTypeError("Minimum grid size is 3 x 3")
return size
def create_word_list_from_file(filehandle, grid_size):
"""Return a list of words to search for"""
word_list = set(line.strip().upper() for line in filehandle if len(line) <= grid_size)
return word_list
if __name__ == "__main__":
main()