13/40
Part 3 of 7 · Two sequencesLearn
Fill row by row from a base row and a base column, for O(nm)
Here is the table it fills for tails and train.
for row in edit_distance("tails", "train"):
print(row)
[0, 1, 2, 3, 4, 5]
[1, 0, 1, 2, 3, 4]
[2, 1, 1, 1, 2, 3]
[3, 2, 2, 2, 1, 2]
[4, 3, 3, 3, 2, 2]
[5, 4, 4, 4, 3, 3]
The function returns the whole table. With a's prefixes down the side and b's along the top, it reads like this:
d ""train""012345t101234a211123i322212l433322s544433
Row a, column a: the letters match, so the cell copies its diagonal, 1. The corner says tails becomes train in 3 edits.
What it costs. There are cells for strings of lengths and , and each one is a comparison and a min of three. That's time and memory. Measured on random strings of the letters A, C, G and T, best of 3 runs:
Double both string lengths. About how much longer does the table take?
Pick an answer above to see the rest