-
Notifications
You must be signed in to change notification settings - Fork 0
/
bee.py
executable file
·78 lines (65 loc) · 1.78 KB
/
bee.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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import os
DICT_FILE = "/usr/share/dict/words"
# From github.com/dwyl/english-words
ALPHA_FILE = os.path.join(os.path.dirname(__file__), "words_alpha.txt")
parser = argparse.ArgumentParser(description="Spelling Bee Finder")
parser.set_defaults(
word_file=ALPHA_FILE,
)
parser.add_argument(
"letters", help="Seven uppercase letters. Center (mandatory) letter first."
)
parser.add_argument(
"--dict",
"-d",
action="store_const",
const=DICT_FILE,
dest="word_file",
help="Use %(const)s",
)
parser.add_argument(
"--alpha",
"-a",
action="store_const",
const=ALPHA_FILE,
dest="word_file",
help="Use %(const)s",
)
namespace = parser.parse_args()
center = namespace.letters[0]
others = {c for c in namespace.letters[1:]}
if len({c for c in namespace.letters}) != 7 or not all(
"A" <= c <= "Z" for c in namespace.letters
):
raise ValueError(
f"Need 7 distinct uppercase letters for Spelling Bee: {namespace.letters!r}"
)
def print_list(label: str, lst: list[str]) -> None:
print("\n{}:\n\t{}".format(label, "\n\t".join(lst)))
with open(namespace.word_file) as f:
WORDS = [w.upper() for w in f.read().splitlines()]
pangrams = []
buzzes = []
for w in WORDS:
if len(w) < 4:
continue
chars = {c.upper() for c in w}
if center not in chars:
continue
if chars - others != {center}:
continue
if len(chars) == 7:
pangrams.append(w)
buzzes.append(w)
buzzes.sort()
previous = [buzzes[0]]
for buzz in buzzes[1:]:
if buzz[:2] != previous[0][:2]:
print_list(previous[0][:2], previous)
previous = []
previous.append(buzz)
print_list(previous[0][:2], previous)
print_list("Pangrams", sorted(pangrams))