import sys

def distance(s1, s2):
    if len(s1) < len(s2):
        return distance(s2, s1)

    if len(s2) == 0:
        return len(s1)

    previous_row = xrange(len(s2) + 1)
    for i, c1 in enumerate(s1):
        current_row = [i + 1]
        for j, c2 in enumerate(s2):
            insertions = previous_row[j + 1] + 1
            deletions = current_row[j] + 1
            substitutions = previous_row[j] + (c1 != c2)
            current_row.append(min(insertions, deletions, substitutions))
        previous_row = current_row

    return previous_row[-1]

def find_matches(dna_segment, dna_sequence, m):
    result = {}
    for i in xrange(len(dna_sequence)):
        for length in xrange(max(1, len(dna_segment)-m), len(dna_segment)+m):
            seg = dna_sequence[i:i+length]

            if len(seg) != len(dna_segment):
                continue

            mismatches = distance(dna_segment, seg)
            if mismatches <= m:
                if mismatches in result:
                    result[mismatches].append(seg)
                else:
                    result[mismatches] = [seg]

    return result

test_cases = open(sys.argv[1], 'r')
for test in test_cases:
    test = test.strip()
    if len(test) == 0:
        continue

    dna_segment = test.split(' ')[0]
    dna_sequence = test.split(' ')[2]
    m = int(test.split(' ')[1])

    result = find_matches(dna_segment, dna_sequence, m)

    if len(result) > 0:
        printable = []
        for mismatches in sorted(result.keys()):
            for seg in sorted(result[mismatches]):
                printable.append(seg)

        print " ".join(printable)
    else:
        print 'No match'

test_cases.close()
@@ -3,16 +3,35 @@
 port sys
+%0Aimport collections
 %0A%0Adef di
@@ -71,24 +71,16 @@
 len(s2):
-%0A       
  return 
@@ -92,25 +92,24 @@
 nce(s2, s1)%0A
-%0A
     if len(s
@@ -116,24 +116,16 @@
 2) == 0:
-%0A       
  return 
@@ -132,17 +132,16 @@
 len(s1)%0A
-%0A
     prev
@@ -527,17 +527,16 @@
 ent_row%0A
-%0A
     retu
@@ -621,63 +621,46 @@
 t = 
-%7B%7D%0A    for i in xrange(len(dna_sequence)):%0A    
+collections.defaultdict(list)%0A
     for 
 leng
@@ -655,22 +655,17 @@
     for 
-length
+i
  in xran
@@ -671,15 +671,8 @@
 nge(
-max(1, 
 len(
@@ -681,44 +681,18 @@
 a_se
-gment)-m), len(dna_segment)+m):%0A    
+quence)):%0A
     
@@ -725,18 +725,23 @@
 +len
-gth%5D%0A%0A    
+(dna_segment)%5D%0A
     
@@ -780,39 +780,15 @@
 nt):
-%0A                continue%0A%0A    
+ break%0A
     
@@ -827,28 +827,24 @@
 gment, seg)%0A
-    
         if m
@@ -862,190 +862,75 @@
 = m:
-%0A                if mismatches in result:%0A                    result%5Bmismatches%5D.append(seg)%0A                else:%0A                    result%5Bmismatches%5D = %5Bseg%5D%0A%0A    return result%0A%0A
+ result%5Bmismatches%5D.append(seg)%0A    return result%0A%0Adef main():%0A    
 test
@@ -961,16 +961,20 @@
 %5D, 'r')%0A
+    
 for test
@@ -989,16 +989,20 @@
 _cases:%0A
+    
     test
@@ -1017,24 +1017,28 @@
 strip()%0A    
+ 
+   
 if len(test)
@@ -1040,24 +1040,28 @@
 test) == 0:%0A
+    
         cont
@@ -1065,17 +1065,20 @@
 ontinue%0A
-%0A
+    
     dna_
@@ -1106,24 +1106,28 @@
 ' ')%5B0%5D%0A    
+ 
+   
 dna_sequence
@@ -1148,16 +1148,20 @@
 ' ')%5B2%5D%0A
+    
     m = 
@@ -1184,17 +1184,20 @@
  ')%5B1%5D)%0A
-%0A
+    
     resu
@@ -1244,17 +1244,20 @@
 nce, m)%0A
-%0A
+    
     if l
@@ -1272,32 +1272,36 @@
 t) %3E 0:%0A        
+   
+ 
 printable = %5B%5D%0A 
@@ -1299,16 +1299,20 @@
 le = %5B%5D%0A
+    
         
@@ -1360,24 +1360,28 @@
             
+ 
+   
 for seg in s
@@ -1407,16 +1407,20 @@
 ches%5D):%0A
+    
         
@@ -1449,17 +1449,20 @@
 nd(seg)%0A
-%0A
+    
         
@@ -1495,14 +1495,22 @@
     
+ 
+   
 else:%0A
+    
     
@@ -1530,17 +1530,20 @@
  match'%0A
-%0A
+    
 test_cas
@@ -1552,8 +1552,48 @@
 .close()
+%0A%0Aif __name__ == '__main__':%0A    main()%0A

Fix solution to the playing with DNA problem.