I am working on the distance function in pset6 similarities (more comfortable). When I run ./score cat ate, it gives me the error "could not read cat". I copied my code to another tab to test out my code, and the answers for matrix are right. I have no clue what the problem is. Please help.
def distances(a, b):
"""Calculate edit distance from a to b"""
h = len(a) + 1
w = len(b) + 1
# set up 2D array
matrix = [[0 for x in range(w)] for y in range(h)]
matrix[0][0] = (0, None)
# add value for base case
for x in range(1, h):
matrix[x][0] = (x, Operation.DELETED)
for y in range(1, w):
matrix[0][y] = (y, Operation.INSERTED)
# add value for other entries
for i in range(1, h):
for j in range(1, w):
cost_del = matrix[i-1][j][0]
cost_ins = matrix[i][j-1][0]
cost_sub = matrix[i-1][j-1][0]
cost = min(cost_del, cost_ins, cost_sub)
if cost == cost_del == cost_sub and a[i-1] != b[j-1]:
matrix[i][j] = (cost_del + 1, Operation.DELETED)
elif cost == cost_ins == cost_sub and a[i-1] != b[j-1]:
matrix[i][j] = (cost_ins + 1, Operation.INSERTED)
elif cost == cost_sub:
if a[i-1] == b[j-1]:
matrix[i][j] = (cost_sub, Operation.SUBSTITUTED)
else:
matrix[i][j] = (cost_sub + 1, Operation.SUBSTITUTED)
elif cost == cost_del:
matrix[i][j] = (cost_del + 1, Operation.DELETED)
else:
matrix[i][j] = (cost_ins +1, Operation.INSERTED)
return matrix