-2

I am running PyCharm version 3.6.6 (I checked with sys.version command) and my interpreter is 3.6. The problem is with all the print function. This is the code for edit distance algorithm my teacher provided for us. We are suppose to just run this code so it should work... Please see the attached image to see the error.

def edit_distance(s1, s2):
m = len(s1) + 1
n = len(s2) + 1

deletes = 1
inserts = 1
substitute = 1

# Initialize the matrix
mTable = {}
for i in range(0, m):
    for j in range(0, n):
        mTable[i, j] = 0

for i in range(0, m):
    # Need to add one line here
    mTable[i,0] = i * deletes

for j in range(0, n):
    # Need to add one line here
    mTable[0, j] = j * inserts

cost = 0

for i in range(1, m):
    for j in range(1, n):
       if(s1[i-1] == s2[j-1]):
           cost = 0
       else:
            mTable[i,j]=min(mTable[i-1,j] + deletes,
            mTable[i, j - 1] + inserts,  # (i,j-1) with insert cost
            mTable[i - 1, j - 1] + cost  # (i-1,j-1) with if match cost=0 else cost=substitude

# Print the edit distance matrix
print("Edit Distance Matrix\n")
print("      ", end= ' ')
for j in range(n-1):
    print("| " + s2[j] + " ", end='')
print("\n")
for i in range(0, m):
    if i == 0:
        print("   ", end='')
    if i > 0:
        print(" " + s1[i - 1] + " ", end='')
    for j in range(0, n):
        print("| " + str(mTable[i, j]) + " ", end='')
    print("\n")

return mTable, mTable[m - 1, n - 1]

def get_edits(s1, s2, mTable, nEditDist):
m = len(s1) + 1
n = len(s2) + 1

i_old = m - 1
j_old = n - 1
i_new = m - 1
j_new = n - 1
sOperation = ""
nIndexOfOperation = nEditDist - 1
sOperationList = {}
for i in range(0, nEditDist - 1):
    sOperationList[i] = ""
while 1:
    nLeft = mTable[i_old, j_old-1]
    nUp   = mTable[i_old-1, j_old]
    nUpLeft = mTable[i_old-1, j_old-1]
    if nUpLeft <= nLeft and nUpLeft <= nUp:
        i_new = i_old - 1
        j_new = j_old - 1
        if mTable[i_old, j_old] > nUpLeft:
            sOperation = "Substitute"                     
            sOperationList[nIndexOfOperation] = sOperation
            nIndexOfOperation -= 1
    elif nLeft <= nUpLeft and nLeft <= nUp:
        i_new = i_old
        j_new = j_old - 1
        if mTable[i_old, j_old] > nLeft:
            sOperation = "Insert"                     
            sOperationList[nIndexOfOperation] = sOperation
            nIndexOfOperation -= 1
    elif nUp <= nUpLeft and nUp <= nLeft:
        i_new = i_old - 1
        j_new = j_old
        if mTable[i_old, j_old] > nUp:
            sOperation = "Delete"                  
            sOperationList[nIndexOfOperation] = sOperation
            nIndexOfOperation -= 1
    i_old = i_new
    j_old = j_new
    if i_old == 0 and j_old == 0:
        break

print("the sequence of the edits:")
for i in range(0, nEditDist):
    print("Step " + str(i + 1) + " : " + sOperationList[i])


if __name__ == "__main__":
# Example 1
sString1 = "kitten"
sString2 = "sitting"
# Example 2
# sString1 = "GAMBOL"
# sString2 = "GUMBO"
mTable, nEditDist = edit_distance(sString1, sString2)
print("Edit distance is " + str(nEditDist))
get_edits(sString1, sString2, mTable, nEditDist)

Here's a picture of the errors:

enter image description here

I tried to include 'from future import print_function' at the top but the error won't go away.

Fran
  • 98
  • 1
  • 12
  • Looks ok to me. Perhaps try unindenting and reindenting using tab instead – Sheldore Sep 24 '18 at 19:33
  • It's your commas in the first line of the top `else:` They shouldn't be there – NickHilton Sep 24 '18 at 19:35
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Images don't do the job. – Prune Sep 24 '18 at 19:35
  • Please copy and paste your code here directly instead of posting a screenshot. – Code-Apprentice Sep 24 '18 at 19:36
  • You have eight print statements. Which one is the problem? – Acccumulation Sep 24 '18 at 19:39
  • 1
    It's a parenthesis issue – Blake Sep 24 '18 at 19:40

1 Answers1

1

You appear to be missing a closing paren towards the beginning of your code:

mTable[i, j] = min(...
    ...
    ... ) <-- on the third line here
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    One of the benefits to using an IDE like PyCharm is that it highlights syntax errors in red. Do you see the first red mark on your screenshot? That's because of the syntax error @Code-Apprentice points out in this (correct) answer. – Blake Sep 24 '18 at 19:38
  • @Code-Apprentice it worked...hehe...don't I feel dumb... – Fran Sep 24 '18 at 19:46
  • @Jen Please take note of Blake's comment. That is exactly what I looked at to determine the problem. Of course, it takes some experience and practice to figure out why the red mark is there. – Code-Apprentice Sep 24 '18 at 19:52
  • @Code-Apprentice thanks I’ll take note of that – Fran Sep 24 '18 at 21:28
  • @Jen When you see red squigglies in the editor, the problem can be anywhere before the first one, so don't focus too much on the exact spot where the first squiggly appears. This is just a starting point. – Code-Apprentice Sep 24 '18 at 21:51