0

This is my first ever question so apologies if there are any errors in how this has been asked, welcome any criticism.

I am having an issue with my function SplitSub in the substring part of the pset.

When I run this with a test,

# Test Print
    print(subB)

Where b contains a textile with just the word "hello" in. I would expect the output: "he" "el" "ll" "lo" for an n value of 2 however i just get "he".

It seems the issue is the function stops after the first two letters?

Please help

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

    # Function to split variable into Substring
    def splitSub(x,n):
        # Formula for number of times string x is split for given n
        splits = (len(x)//n + 2)
        for i in range(splits):
            return x[i:i+n]

    # Split each string a,b into all substrings of length n
    subA = splitSub(a,n)
    subB = splitSub(b,n)

    # Test Print
    print(subB)

    # Compute list of all substrings appearing in both a and b
    mylist = []
    for substring1 in subA:
        for substring2 in subB:
            if substring1 == substring2:
                mylist = substring1

    # Return the list
    return [mylist]

1

Your issue is here

for i in range(splits):
    return x[i:i+n]

Remember that return ends the function immediately, so you are returning the result after only the first time the loop runs.

Perhaps create an empty list that you append to each time through the loop and then return that list after the loop finishes.

You must log in to answer this question.

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