-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathAnagram_Set.Py
62 lines (48 loc) · 1.4 KB
/
Anagram_Set.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
'''Playing With Anagrams'''
def signature(s):
li=list(s)
li.sort()
li=''.join(li)
return li
'''Signature is a string that contains all of the letters in order.
print(signature('sahil'))
output : ahils'''
def all_anagrams(filename):
d={}
fin=open(filename)
for line in fin:
word=line.strip().lower()
t=signature(word)
if t not in d:
d[t]=[word]
else:
d[t]+=[word]
return d
def print_anagram_sets_in_order(d):
'''prints anagram sets in desc order of size'''
#make a list of (length,word pairs)
t=[]
for v in d.values():
if len(v)>1:
t.append((len(v),v))
'''Here, t is a list of tuple(int,list of str)
in tuple first one is length of 'list of str' and a 'list of str'
Example:----
(2,['aah','aha']) is one of the element of t.
which is tuple.'''
#sort in desc order of length
t.sort(reverse=True)
for x in t:
print(x)
def filter_length(d,n):
'''returns a dict of anagrams having length=n'''
res={}
for word,anagram in d.items():
if len(word)==n:
res[word]=anagram
return res
if __name__ == '__main__':
anagram_map = all_anagrams('words.txt')
print_anagram_sets_in_order(anagram_map)
eight_letters = filter_length(anagram_map, 8)
#print_anagram_sets_in_order(eight_letters)