-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram_qns.py
More file actions
64 lines (56 loc) · 1.22 KB
/
anagram_qns.py
File metadata and controls
64 lines (56 loc) · 1.22 KB
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
#! /usr/bin/python2.7
def signature(s):
t = list(s)
t.sort()
t = ''.join(t)
return t
def all_anagrams(filename):
d = {}
for line in open(filename):
word = line.strip().lower()
t = signature(word)
if t not in d:
d[t] = [word]
else:
d[t].append(word)
return d
def list_anagrams(d):
l = d.values()
l.sort()
print l
def for_scrabble(d):
l = d.values()
ctr = []
for w in l:
if len(w[0]) == 8:
ctr.append(len(w))
maximum = max(ctr)
for w in l:
if len(w[0]) == 8 and len(w) == maximum:
print w
def count(s1,s2):
ctr = 0
for i in range(len(s1)):
if s1[i] != s2[i]:
ctr += 1
if ctr == 2:
return True
return False
def compare(words):
s = []
for s1 in words:
for s2 in words:
if count(s1,s2):
if [s2,s1] not in s:
s.append([s1,s2])
if len(s) > 1:
print s
def meta_pair(d):
l = d.values()
for words in l:
if len(words) > 1:
compare(words)
d = all_anagrams('words.txt')
#list_anagrams(d)
#for_scrabble(d)
meta_pair(d)