-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon_solver.py
82 lines (62 loc) · 1.98 KB
/
polygon_solver.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import itertools
import json
import sys
dict_list = ["dictionary1.json", "dictionary2.json"]
DICTIONARY = 1
## COMMAND LINE FORMAT
# python polygon_solver.py (str) available_letters (char) required_letter (int) min_size
def main():
# error command line arguments
argc = len(sys.argv)
dictionary = dict_list[DICTIONARY]
if argc <= 3:
print("too few args")
return 1
# if argc > 4 and "true" == sys.argv[4]:
# dictionary = dict_list[0]
available_letters = sys.argv[1].upper()
required_letter = sys.argv[2].upper()
min_size = sys.argv[3]
max_size = len(available_letters)
debug = False
if argc >= 5:
debug = True
if debug:
print(available_letters, required_letter, min_size)
if len(required_letter) != 1:
print("required must be one")
return 1
try:
min_size = int(min_size)
except:
print("cannot make min size an int")
return 1
# start program
with open(dictionary) as f:
all_words = json.load(f)
if debug:
all_words["hello"]
def validate(word):
if not required_letter in word:
return False
if not word in all_words:
return False
return True
valid = []
for i in range(min_size, max_size+1):
i_perms = list(itertools.permutations(available_letters, i))
i_perms = sorted(list(set(map(lambda x : x.lower(), list(filter(validate, map(lambda x : "".join(x), i_perms)))))))
valid.extend(i_perms)
if len(i_perms) > 0:
print(i,":",end=" ")
for perm in i_perms:
print(perm.capitalize(), end=", ")
print()
else:
print(i,":","NONE FOUND")
# valid = sorted(list(set(filter(validate, map(lambda x : "".join(x), perms)))), key=len)
print(len(valid), "Words Found")
# print(list(map(lambda x : x.lower(), valid)))
main()
# LETTERS = "grievpeil"
# REQUIRED = 'l'