-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen2.py
181 lines (159 loc) · 4.44 KB
/
gen2.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from itertools import combinations
'''
Author: Ethan Roland
Date: 4/30/19
Produces all possible string mixes
'''
def mix(str1, str2):
print('---')
#generates EditDist array
m = len(str1)
n = len(str2)
dp = [[0 for x in range(n+1)] for x in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if i == 0:
dp[i][j] = j # Min. operations = j
elif j == 0:
dp[i][j] = i # Min. operations = i
elif str1[i-1] == str2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = 1 + min(dp[i][j-1], # Insert
dp[i-1][j], # Remove
dp[i-1][j-1]) # Replace
'''
#prints ED array
print('',end='\t\t')
for let in str2:
print(let,end='\t')
print()
for i in range(m+1):
if i > 0:
print(str1[i-1],end='\t')
else:
print('',end='\t')
for j in range(n+1):
print(dp[i][j],end='\t')
print()
print('---')
'''
print('rank is\t\t\t\t\t',dp[m][n])
#gives paths from s1->s2
paths = []
path = []
gen(paths,path,' '+str1,' '+str2,dp,m,n,str2)
#gives all combinations of paths with len = lim
lim = (int)(dp[m][n]/2)
moves = set()
for el in paths:
#print(el)
comb = combinations(el,lim)
for c in comb:
#print(c)
moves.add(c)
#print(len(moves))
#executes calculated moves
out = set()
for el in moves:
#print(el)
word = str2
temp = []
for m in el:
if '+' not in m[1]:
word = word[:m[0]] + m[1] + word[m[0]+1:]
else:
temp.append(m)
d = 0
for m in temp:
#print(word,m)
word = word[:m[0]+d] + m[1][1] + word[m[0]+d:]
d += 1
word = word.replace('-','')
out.add(word)
print('mixed words, lm =',lim,'\t',len(out))
out2 = set()
#determines if gen'd word is acceptable
for el in out:
if proper(el,str1,str2):
out2.add(el)
print('accepted words \t\t\t',len(out2))
print('---')
return out2
def gen(paths,path,s1,s2,arr,x,y,word):
#print('----',x,y,word)
if arr[x][y] == 0:
#print('RETURN')
#print(path)
paths.append(path)
return
cur = arr[x][y]
try:
lf = arr[x][y-1]
except:
lf = 1000
try:
up = arr[x-1][y]
except:
up = 1000
try:
di = arr[x-1][y-1]
except:
di = 1000
#if letters are the same
if s1[x] == s2[y]:
#print('same')
gen(paths,path,s1,s2,arr,x-1,y-1,word)
return
#finds all minimum next steps
next = {(x,y-1):lf}
if up == lf:
next[(x-1,y)] = up
elif up < lf:
next.clear()
next[(x-1,y)] = up
if di == min(lf,up):
next[(x-1,y-1)] = di
elif di < min(lf,up):
next.clear()
next[(x-1,y-1)] = di
#for m in next.keys():
# print(m)
#performs edits
for m in next.keys():
#print(x,y,'-->',m[0],m[1])
if m[0] == x-1 and m[1] == y-1: #diagonal/replace
temp = word[:y-1] + s1[x] + word[y:]
p2 = path.copy()
p2.append((y-1,s1[x]))
#print('di/replace',temp)
gen(paths,p2,s1,s2,arr,x-1,y-1,temp)
if m[0] == x-1 and m[1] == y: #up/insert
temp = word[:y] + s1[x] + word[y:]
p2 = path.copy()
p2.append((y,'+'+s1[x]))
#print('up/insert',temp)
gen(paths,p2,s1,s2,arr,x-1,y,temp)
if m[0] == x and m[1] == y-1: #left/delete
temp = word[:y-1] + word[y:]
p2 = path.copy()
p2.append((y-1,'-'))
#print('left/delete',temp)
gen(paths,p2,s1,s2,arr,x,y-1,temp)
return
def proper(input,s1,s2) :
if input[0] != s1[0] and input[0] != s2[0]:
return False
#return True
v = 0
c = 0
for let in input:
if let in 'aeiou':
c = 0
v += 1
else: #is a consonant
v = 0
c += 1
if c > 2 or v > 1:
return False
return True