heres my code

from operator import itemgetter

class Operation(Enum):

    DELETED = 1
    INSERTED = 2

    def __str__(self):
        return str(self.name.lower())

def distances(a, b):
    """Calculate edit
    distance from a to b"""

    len_a = len(a)
    len_b = len(b)

    twod = [[ ("0","none") for j  in range(len_b + 1)] for i in range(len_a + 1)]

    for i in range(1,len_a + 1 ):
        twod[i][0] = (i,"D")

    for j in range(1,len_b + 1 ):
        twod[0][j] = (j,"I")

    for i in range(1,len_a + 1):
        for j in range(1 , len_b + 1):

            # what woud be cost if the twod[i][j] is taken from twod[i-1][j-1]
            if (a[i] == b[j]):
                c = twod[i-1][j-1][0]
                c = twod[i-1][j-1][0] + 1

            # writing a temporary list which could directly give the operation based on the position of min cost
            arr_list = [(twod[i-1][j][0]+1,'D'),(twod[i][j-1][0]+1,'I'),(c,'S')]

            # getting the (cost,operation) into twod[i][j] based on min cost
            twod[i][j] = min(arr_list, key=lambda x:x[0])

    return twod

here is the error

~/similarities/ $ ./score LesMis1.txt LesMis2.txt
Traceback (most recent call last):
  File "./score", line 35, in <module>
  File "./score", line 30, in main
    d = distances(file1, file2)
  File "/home/ubuntu/similarities/helpers.py", line 43, in distances
    twod[i][j] = min(arr_list, key=lambda x:x[0])
TypeError: '<' not supported between instances of 'str' and 'int'

1 Answer 1


Haven't seen this in a while, as it is not part of the current CS50x 2019.

This particular issue is with ("0","none"), you probably meant (0, None) (None is a special value, describing the absence of something).

Unrelated, you shouldn't use "D" but Operation.DELETED.

And you should if (a[i - 1] == b[j - 1]): (you start the loop with the index at 1, but want to start comparing the string at index 0).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .