def f(t,a=range(44)): for e in s[0]: \x09b=a[0]+1, \x09for f,g,h in zip(t,a,a[1:]):b+=min(h,b[-1],g-(e==f))+1,;a=b return b[-1] s=list(open('../fd/0')) print''.join(sorted(s,key=f))
Note that non-ascii characters in the above source code will be escaped (such as \x9f).