-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalternate_version.py
40 lines (33 loc) · 1.06 KB
/
alternate_version.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
try:
U = input("Enter U: ")
except SyntaxError:
U = ""
try:
V = input("Enter V: ")
except SyntaxError:
V = ""
try:
W = input("Enter W: ")
except SyntaxError:
W = ""
print(len(U), len(V), len(W))
if len(W) != (len(U) + len(V)):
print(W, "is NOT an interleaving of", U, "and", V)
graph = [[0 for x in range(0, len(U) + 2)] for y in range(0, len(V) + 2)]
graph[0][0] = True
# the edges along the 'outside' of the graph have one parent each, so set them specially
for i in range(1, len(U)):
graph[0][i] = graph[0][i - 1] and U[i] == W[i]
for j in range(1, len(V)):
graph[j][0] = graph[j - 1][0] and V[j] == W[j]
for i in range(0, len(U) - 1):
for j in range(0, len(V) - 1):
print("i: ", i, " j: ", j)
graph[i + 1][j + 1] = (graph[i + 1][j] and U[i] == W[i + j]) or (
graph[i][j + 1] and V[j] == W[i + j]
)
print("v: ", len(V), " u: ", len(U))
if graph[len(U) - 1][len(V) - 1] == True:
print(W, " is an interleaving of ", U, " and ", V)
else:
print(W, " is NOT an interleaving of ", U, " and ", V)