-
Notifications
You must be signed in to change notification settings - Fork 2
/
WordGame.py
59 lines (43 loc) · 1.18 KB
/
WordGame.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
from itertools import permutations
import time
import Trie
def extractfile(file_name):
keys=set()
with open(file_name,"r") as f:
for data in f:
data =''.join(c for c in data if 65 <= ord(c) <= 90 or 97 <= ord(c) <= 122)
keys.add(data)
return keys
def parse_file(file):
while True:
try:
keys = extractfile(file)
print('Loading...')
return keys
except:
print("Keep the 'dictionary.txt' in the same folder of this file")
time.sleep(10)
continue
def solution(t):
while True:
letters = list(i for i in input('Enter the letters: ').strip())
try:
length = int(input('Enter the required length of word: '))
except:
print('Retry...')
continue
a=set()
for word in permutations(letters,length):
a.add(''.join(word))
for i in a:
if t.search(i):
print(i)
print()
def main():
keys=parse_file('dictionary.txt')
t=Trie.Trie()
for key in keys:
t.insert(key)
solution(t)
if __name__=='__main__':
main()