-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.py
42 lines (38 loc) · 963 Bytes
/
search.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
from itertools import combinations
from bisect import bisect_left
def loadanadict():
#f = open("anadict.txt")
#anadict = f.read().split('\n')
#f.close()
anadict = {}
with open('anadict.txt','r') as txt:
for line in txt:
items = line.split()
key,values = items[0], items[1:]
anadict.setdefault(key,[]).extend(values)
return anadict
def findwords(rack, anadict):
rack = ''.join(sorted(rack))
foundwords = set()
for i in range(3,len(rack)+1):
for comb in combinations(rack,i):
anagram = ''.join(comb)
if anagram in anadict:
for word in anadict[anagram]:
foundwords.add(word)
# if (anagram in anadict):
# foundwords.append(anadict[anagram])
return foundwords
import sys
if len(sys.argv)==2:
rack = sys.argv[1].strip()
else:
print("Inserisci lettere")
exit()
anadict = loadanadict()
#print (anadict)
result = (findwords(rack, anadict))
result = list(result)
result.sort( key = len)
for word in result:
print (word)