i, j = len(s1), len(s2)
while True:
_, operation = matrix[i][j]
if not operation:
break
if operation == Operation.INSERTED:
j -= 1
elif operation == Operation.DELETED:
i -= 1
else: #operation substitute
i -= 1
j -= 1
operations.append(operation)
operations.reverse()
# Maintain list of intermediate strings, operation, and descriptions
transitions = [(s1, None, None)]
i = 0
# Apply each operation
prev = s1
for operation in operations:
# Update string and description of operation
if operation == Operation.INSERTED:
s = (prev[:i], s2[i], prev[i:])
description = f"inserted '{s2[i]}'"
prev = prev[:i] + s2[i] + prev[i:]
i += 1
elif operation == Operation.DELETED:
s = (prev[:i], prev[i], prev[i + 1:])
description = f"deleted '{prev[i]}'"
prev = prev[:i] + prev[i + 1:]
elif prev[i] != s2[i]:
s = (prev[:i], s2[i], prev[i + 1:])
description = f"substituted '{prev[i]}' with '{s2[i]}'"
prev = prev[:i] + s2[i] + prev[i + 1:]
i += 1
else:
i += 1
continue
transitions.append((s, str(operation), description))
transitions.append((s2, None, None))
I'm not sure what the "_," means. It appears that the first "while true" statement will decrement i and j all the way back to zero, which makes sense, since they are just moving back up the road, if you will. The true
makes sense, we just break
later. But I still don't understand the underscore. Also, what does prev[:i]
mean?
Thanks a lot! (I already have a working similarities algorithm, I wrote that in the console.