0

I am currently working on PSet7: Similarities. I have completed all the tests so far except substrings and I was wondering if any of you could tell me where I am going wrong. I am currently failing all the tests on the substrings portions. Thank you!

def substrings(a, b, n):
    """Return substrings of length n in both a and b"""
    truelines = []
    asubstrings = extract(a, n)
    bsubstrings = extract(b, n)
    for i in range(0, len(asubstrings)):
        for j in range(0, len(bsubstrings)):
            if asubstrings[i] == bsubstrings[j]:
                if asubstrings[i] not in truelines:
                    truelines.append(asubstrings[i])
    return truelines

def extract(string, num):
    substrings = []
    for i in range(0, len(string)):
        substrings.append(string[i:(i+num)])
    return substrings
1

Check the list extract returns. Its last elements are a bit too short, as there are no len(string) substrings of length num, their number also depends on num itself. You should probably adjust your loop.

  • So I solved my length problem by changing for i in range(0, len(string)): to for i in range(0, len(string) - (num): I'm still not passing the tests. Where else can I be going wrong? Thank you so much for your help – Jacob Pieczynski Jul 9 '19 at 15:56
  • 1
    len(string) - num is slightly off. There's a + 1 missing. Imagine len(string) and num to be the same - there would be exactly one possible substring. – Blauelf Jul 9 '19 at 16:51
  • That worked! Thank you :) – Jacob Pieczynski Jul 9 '19 at 20:10

You must log in to answer this question.

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