Edit distance specifc implementation for number . PYTHON
<p>I got a question on code chef asking the modified version of edit distance , where it is edit distance between 2 numbers , where only deletion operation is allowed and deletion cost = deleted number .</p>

<p>So I tried this to implement on python . But getting </p>

<pre><code>TypeError: 'function' object has no attribute '__getitem__' 
</code></pre>

<p>I am getting this error in the last line i.e return line in EditDisRec function</p>

<p>Here is the code</p>

<pre><code>def ISTHERE(x,y):
    a=len(str(y))
    b=y
    for i in range(0,a):
        if b%10 == x:
            return(True)
            break
        else:
            b=b/10
    return(False)            


def sum_digits(N):
    n=int(N)
    s = 0
    while n:
        s += n % 10
        n //= 10
    return s
def delt(x,y):
    if int(x)==int(y):
        return 0
    else:
        return int(x)+int(y)

def EditDistRec(S,T):
    if S==0:
        return sum_digits(T)
    elif T==0:
        return sum_digits(S)
    elif (S==1 or S==2 or S==3 or S==4 or S==5 or S==6 or S==7 or S==8 or S==9 ):
        if ISTHERE(S,T)==True :
            return sum_digits(T) - S 
        elif ISTHERE(S,T)==False:
            return sum_digits(T)
    elif (T==1 or T==2 or T==3 or T==4 or T==5 or T==6 or T==7 or T==8 or T==9 ):
        if ISTHERE(T,S)==True :
            return sum_digits(S) - T 
        elif ISTHERE(T,S)==False:
            return sum_digits(S) 


    return min(EditDistRec(S/10,T/10) + delt[S%10,T%10],(EditDistRec(S ,T/10) + int(T%10)),(EditDistRec(S/10,T ) + int(S%10)))


print(EditDistRec(7315,713))
</code></pre>

<p>you have typo here  <code>delt[S%10,T%10]</code> </p>

<p>This means that you cannot index them like a list because <strong>getitem</strong> is the method which handles this operation.<code>delt</code> is a function, not a list </p>

<p>Corrected: </p>

<pre><code>return min(EditDistRec(S/10,T/10) + delt(S%10,T%10),(EditDistRec(S ,T/10) + int(T%10)),(EditDistRec(S/10,T ) + int(S%10)))
</code></pre>

<p>Output:</p>

<pre><code>sh-4.3$ python main.py                                                             
7
sh-4.3$
</code></pre>

python python-2.7