0

My code is as follows def substrings(a, b, n): """Return substrings of length n in both a and b""" similar = [] lista = [] listb = [] crawla = 0 crawlb = 0 while (crawla + n) < len(a): # While there are substrings left to iterate lista.append(a[crawla:crawla + n]) # Starting from 0, append chars from crawla to crawla + n crawla += 1 while (crawlb + n) < len(b): listb.append(b[crawlb:crawlb + n]) crawlb += 1 for sbstr in lista: if sbstr in listb and sbstr not in similar: similar.append(sbstr) # TODO return (similar) # Returns similar sentences

It seems to work as intended for the most part, however the amount of matching substrings I'm returning is inaccurate. I'm not really sure why as when I tried going over it manually in my head it seems accurate.

https://cs50.me/checks/e890fce3fd8ede412a1136859277e487617bd56e

0

Use <= len(a) and <= len(b). You always ignore that last substring, that's why "bar" isn't part of "foobar".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .