0

I'm almost there with Similarities but I just can't seem to get past the last few substring tests. I am passing all tests except the last four:

  1. :( handles multiple substrings in common, Expected 4 matches, not 0
  2. :( handles substrings when strings are identical, Expected 4 matches, not 2
  3. :( handles substring length longer than string length, Expected 0 matches, not 1
  4. :( handles duplicate substrings in common, Expected 4 matches, not 1

I've tested my code through the flask server and it all seems to work great through index.html. . .substrings are highlighted, all is well. Here's my substring code below:

def substrings(a, b, n):
"""Return substrings of length n in both a and b"""

# TODO
aSub = [a[i:i+n] for i in range(0, len(a), n)] # use slice with a range of the word's len to take 'n' chunks from word
bSub = [b[i:i+n] for i in range(0, len(b), n)] # use slice with a range of the word's len to take 'n' chunks from word

sameSub = [] # make an empty list to store similar substrings in
for x in aSub: # iterate through list A
    for y in bSub: # iterate through list B
        if x == y: # are the sentences the same? Then put the sentence in a new list (sameSent)
            sameSub.append(x)

# Remove duplicate lines by pusing the list into a set
simSub = list(set(sameSub))

return simSub

Just can't seem to figure out why I can't pass the tests. Any help is greatly appreciated.

Thanks!

1 Answer 1

1

The substrings can overlap. So the word "string" would have 4 substrings of length 3, "str", "tri", "rin", and "ing".

Also, make sure to only have substrings that have the full length (by having your range of the right length).

2
  • This was the answer. Thank you! (and are you the same guy from reddit? double thank you!)
    – snejame
    Commented Jun 4, 2019 at 17:31
  • Too lazy to invent new nicks everywhere, so I go by that name on many platforms (like reddit). But not all that go by that name are me.
    – Blauelf
    Commented Jun 4, 2019 at 19:50

You must log in to answer this question.

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