0
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.

0

According to spec:

Stored in each element of the matrix should be a tuple, (cost, operation), where cost is an int and operation is an Operation.

So what does the line of code mean?. Means that we assign the values of the tuple to two variables but the first of them is ignored, only the second value is of interest to us. Sometimes this technique to ignore specific values is called "I do not care" and is a little-known curiosity of python.

Regarding prev [: i] is not more than a slice, a matrix that goes from the beginning (index zero) to the index i (not including)

In response to Blauelf's comment, I do not find official documentation, that is, a Python feature (I'm not a great python expert, but rather an apprentice). I would say that, more than a convention, it is a technique used by more expert programmers than me, to see clearly how it works we can see it with a simpler example:

# Ignore a value when unpacking
x, _, y = (1, 2, 3) # x = 1, y = 3 

# Ignore the multiple values.
x, *_, y = (1, 2, 3, 4, 5) # x = 1, y = 5 
  • As far as I understand, the "ignore this" usage of the _ variable is just convention, not a property of Python, though it would be surprising to see it used in a different way. Or could you maybe find official documentation mentioning that use? – Blauelf Jan 10 '19 at 7:28
  • I do not know if the use of underscore is mentioned in the official documentation, but it is easy to find many references of its use in google – MARS Jan 10 '19 at 19:08

You must log in to answer this question.

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