-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnearly_equal.py
executable file
·69 lines (56 loc) · 1.36 KB
/
nearly_equal.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
def permutate(word):
if not word:
return [word]
else:
temp = []
for i in range(len(word)):
part = word[:i] + word[i+1:]
for m in permutate(part):
st = word[i:i+1] + m
if st not in temp:
temp.append(st)
return temp
def mutate(word,search=None):
chars = 'abcdefghijklmnopqrstuvwxyz'
l = [i for i in word]
#adding one extra char to the word
comb1 = []
for c in chars:
comb1 += permutate(word+c)
if search and search in comb1:
return True
#replaceing any one char in word with other
comb2 = []
for i in range(len(word)):
temp = l[:]
temp.remove(temp[i])
for c in chars:
comb2 += permutate("".join(temp)+c)
if search and search in comb2:
return True
comb3 = []
#remove one single element from the word
for i in range(len(word)):
temp = l[:]
temp.remove(temp[i])
comb3 += permutate("".join(temp))
if search and search in comb3:
return True
elif search:
return False
return comb1+comb2+comb3
def nearly_equal(word,word1):
if word == word1:
print "both are equal"
return
elif len(word) > len(word1):
word,word1 = word1,word
#words_list = mutate(new)
if mutate(word,word1):
print "Both are nearly equal"
else:
print "Both are not equal"
if __name__ == "__main__":
import sys
#mutate(sys.argv[1])
nearly_equal(sys.argv[1],sys.argv[2])