-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcwf.py
executable file
·124 lines (88 loc) · 2.06 KB
/
cwf.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/env python
# Matt George
# http://en.wikipedia.org/wiki/Beerware
from curses.ascii import isupper, islower
import string
f = open('words.twl')
words = set()
for word in f:
if word[0].isupper():
continue
if word[-1] == "\n":
words.add(word[:-1])
else:
words.add(word)
f.close()
scores = [
('eaionrtlsu', 1),
('dg', 2),
('bcmp', 3),
('fhvwy', 4),
('k', 5),
('jx', 8),
('qz', 10),
(' ', 0)
]
import re
import operator
def do_search(letters, line):
extra = ''
for c in line:
if c.islower():
extra += c
allowed = letters + extra
results = set()
pattern = re.compile(line, flags=re.IGNORECASE)
for word in words:
if re.search(pattern, word) is None:
continue
addit = True
curr_allowed = allowed
curr_score = 0
display_word = word
for c in word:
if c not in curr_allowed:
if ' ' in curr_allowed:
display_word = display_word.replace(c,c.upper(),1)
c = ' '
else:
addit = False
break
for (scorestr, score) in scores:
if c in scorestr:
curr_score += score
break
curr_allowed = curr_allowed[:curr_allowed.index(c)] + curr_allowed[curr_allowed.index(c)+1:]
if addit:
results.add((display_word, curr_score))
return(results)
def print_results(results):
for word, score in sorted(results, key=operator.itemgetter(1)):
print("%5d %10s" % (score, word))
print
def change_letters(letters):
print("Letters set to '%s'\n" % letters)
print("8-letter words:")
print_results(do_search(letters + ' ', '^.{8}$'))
print("7-letter words:")
print_results(do_search(letters, '^.{7}$'))
print
return letters
letters = ''
while True:
line = input('? ')
line = line.lower()
if line == '':
continue
if line[0] == '=':
letters = change_letters(line[1:])
continue
if line[0] == '+':
letters = change_letters(letters + ' ')
continue
if line[0] == '-':
if ' ' in letters:
letters = change_letters(letters[:letters.index(' ')] + letters[letters.index(' ')+1:])
continue
results = do_search(letters, line)
print_results(results)