Levenshtein Distance Sort FIXED by test

\x0d
def g(s, t):\x0d
\x0d
\x09m = [[0] * (len(t) + 1) for _ in range(len(s) + 1)]\x0d
\x0d
\x09for i in range(len(s) + 1):m[i][0] = i\x0d
\x09for j in range(len(t) + 1):m[0][j] = j\x0d
\x0d
\x09for i in range(1, len(s) + 1):\x0d
\x09\x09for j in range(1, len(t) + 1):\x0d
\x0d
\x09\x09\x09if s[i - 1] == t[j - 1]: m[i][j] = m[i - 1][j - 1]\x0d
\x09\x09\x09else:\x0d
\x09\x09\x09\x09m[i][j] = min (\x0d
\x09\x09\x09\x09\x09m[i - 1][j] + 1,\x09\x09# deletion\x0d
\x09\x09\x09\x09\x09m[i][j - 1] + 1,\x09\x09# insertion\x0d
\x09\x09\x09\x09\x09m[i - 1][j - 1] + 1\x09\x09# substitution\x0d
\x09\x09\x09\x09)\x0d
\x0d
\x09return  m[-1][-1]\x0d
\x0d
\x0d
a = raw_input()\x0d
\x0d
l = []\x0d
try:\x0d
\x09while 1:l += [raw_input()]\x0d
except:pass\x0d
\x0d
\x0d
print a\x0d
for s in sorted(l, key=lambda x:g(a, x)):\x0d
\x09print s\x0d

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return top