Hi,<br><br><div class="gmail_quote">On 10 November 2011 16:23, lina <span dir="ltr">&lt;<a href="mailto:lina.lastname@gmail.com">lina.lastname@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

def LongestCommonSubstring(S1, S2):<br>
        M = [[0]*(1+len(S2)) for i in range(1+len(S1))]<br>
        longest, x_longest = 0, 0<br>
        for x in range(1,1+len(S1)):<br>
                for y in range(1,1+len(S2)):<br>
                        M[x][y] = M[x-1][y-1]+1<br>
                        if M[x][y] &gt; longest:<br>
                                longest = M[x][y]<br>
                                x_longest = x<br>
                        else:<br>
                                M[x][y] = 0<br>
        return S1[x_longest-longest:x_longest]<br></blockquote><div><br>This is not the same as the implementation given on wikibooks.... Have you tried reverting your changes and using the coe that was given on the site exactly as is?  (I assume not, and if so, why not?)<br>
<br>(Specifically, I notice most the likely culprit is a missing if statement just below the &quot;for y in range...&quot; line that&#39;s been deleted....)<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

The results isn&#39;t right.<br></blockquote><div><br>Yes.  You appear to have introduced a bug by not using the same code as what was given on the wiki page.  (Why did you modify the code and then when the modified code didn&#39;t work assume the original solution was broken instead of checking first and/or suspecting that your changes may have broken it?) <br>
<br>Walter<br></div></div><br>