[Tutor] longest common substring

lina lina.lastname at gmail.com
Fri Nov 11 15:18:03 CET 2011


<snip>

Based on former advice, I made a correction/modification on the below code.

1] the set and subgroup does not work, here I wish to put all the
subgroup in a big set, the set like
$ python3 LongestCommonSubstring.py | uniq
{"1',"}
{"1', "}
{"1', '"}
{"1', '8"}
{"1', '82"}
{"1', '82'"}
{"1', '82',"}
{"1', '82', "}
{"1', '82', '"}
{"6', '61', '6"}
{"', '61', '63'"}
{"', '61', '63',"}
{"', '61', '63', "}
{"', '61', '63', '"}
{"', '61', '63', '6"}
{"', '61', '70', '61"}
{"', '61', '70', '61'"}
{"', '83', '61', '83',"}
{"', '83', '61', '83', "}
{"', '83', '61', '83', '"}

Please kindly notice I added a pipeline with uniq at the end, the true
prints were lots of replications, I don't know how to handle it in the
python code.

2] I still have trouble in reading files, mainly about not read "" etc.

Thanks with best regards,

#!/usr/bin/python3

import os.path

xrange = range

subgroups=[]
subgroup=[]
def LongestCommonSubstring(S1, S2):
    M = [[0]*(1+len(S2)) for i in xrange(1+len(S1))]
    longest, x_longest = 0, 0
    for x in xrange(1,1+len(S1)):
        for y in xrange(1,1+len(S2)):
            if S1[x-1] == S2[y-1]:
                M[x][y] = M[x-1][y-1]+1
                if M[x][y] > longest:
                    longest = M[x][y]
                    x_longest = x
                if longest >= 3:
                    subgroup=S1[x_longest-longest:x_longest]
                    subgroups=set([subgroup])
                    print(subgroups)
            else:
                    M[x][y] = 0

    return S1[x_longest-longest:x_longest]


if __name__=="__main__":

    a=open("atom-pair_4.txt","r").readline().strip()

    b=open("atom-pair_8.txt","r").readline().strip()

    LongestCommonSubstring(a,b)


More information about the Tutor mailing list