0

I'm coding a program, a part of the program is that I want to create a list with all the substring from a string, using a recursive function.

However, when I return the list, I get nothing. The variable substringList has None value.

How can I return the list, without losing all the data in it?

def main(string):
    substringList = []
    substringList = substring(string, substringList)

def substring(string, substringList):#Recursive function to create all the
    length = len(string)             #substrings**strong text**

    if length == 0:
        return substringList

    else:
        substringList.append(string)
        substring(string[1::], substringList)


string = "bananas"
main(string)
AMC
  • 2,642
  • 7
  • 13
  • 35
AggonyAchilles
  • 17
  • 1
  • 1
  • 7
  • Can you show what output you are expecting? – Cory Kramer Dec 03 '15 at 22:45
  • Please clarify your problem. The way I read it, the answer below doesn't work, as it returns only the substrings that end with the final letter -- which, granted, is the logic of your original code. – Prune Dec 04 '15 at 00:25
  • Does this answer your question? [Recursive function returning none in Python](https://stackoverflow.com/questions/19215141/recursive-function-returning-none-in-python) – AMC Apr 11 '20 at 00:21

4 Answers4

6

You got "None" value because you forgot to use the return command. Also, why are you writing a separate wrapper function to call your recursive function? You can do that easily enough in the main program. You can list the default value of substringList in the calling profile with =[]. New code:

def substring(string, substringList=[]):
    # Recursive function to create all the substrings
    #   of the given string

    if len(string) == 0:
        return substringList

    else:
        substringList.append(string)
        substring(string[1:], substringList)
        return substringList

print substring("bananas")

Now, note that you also haven't written logic to get all of the substrings: you've taken only the ones ending with the final letter. The way you stated the problem, you need the others as well, such as "nan", "n", etc. I hope that's what you're attacking next. Note that you might want more recursion: a second call that finds what you get from chopping off the end of this list instead. Is that enough of a hint to get you going?

Prune
  • 76,765
  • 14
  • 60
  • 81
  • 1
    Note: this approach (ab-)uses the fact that Python only evaluates the default values for named arguments at function definition time, which is why `substringList` does not get overwritten in the subsequent recursive calls and can be shared across the call stack. – Christabella Irwanto Nov 10 '18 at 18:13
2

Is this what you were looking for?

def main(string):
    substringList = []
    substringList = substring(string, substringList)
    return substringList
def substring(string, substringList):#Recursive function to create all the
    length = len(string)             #substrings**strong text**

    if length == 0:
        return substringList

    else:
        substringList.append(string)
        substring(string[1::], substringList)
        return substringList


string = "bananas"
main(string)

>>>['bananas', 'ananas', 'nanas', 'anas', 'nas', 'as', 's']
Riet
  • 1,240
  • 1
  • 15
  • 28
  • Yes, thanks a lot! I see my mistake now, as other people said in the comments, I only return the substring with the final letter. I have to return them all. – AggonyAchilles Dec 04 '15 at 07:42
  • @AggonyAchilles You're welcome and welcome to SO. If you find an answer useful, vote it up. Be sure to accept the best answer, too. That's a part of how we say thanks here. I would recommend Prune's answer as the best – Riet Dec 04 '15 at 13:30
2

Prune's answer above has an issue; however, I have yet to earn enough points to comment so here goes. If you run the following the result from the second call will include the results of the first call concatenated with the first call. I.e.

def substring(string, substringList=[]):
    # Recursive function to create all the substrings
    #   of the given string

    if len(string) == 0:
        return substringList

    else:
        substringList.append(string)
        substring(string[1:], substringList)
        return substringList

print (substring("bananas"))
print (substring("two"))

Results in :

['bananas', 'ananas', 'nanas', 'anas', 'nas', 'as', 's'] ['bananas', 'ananas', 'nanas', 'anas', 'nas', 'as', 's', 'two', 'wo', 'o']

gordonC
  • 79
  • 7
0

I think below is a simpler solution to your problem and also fixes the issue pointed out by gordonC.

def substring(stringList):
    # Recursive function to create all the substrings of the given string
    last_str = stringList[-1]
    if len(last_str) == 1:
        return stringList
    else:
        return substring(stringList + [last_str[1:]])


print(substring(["bananas"]))
print(substring(["two"]))
print(substring(["what"]))

The output is

['bananas', 'ananas', 'nanas', 'anas', 'nas', 'as', 's']
['two', 'wo', 'o']
['what', 'hat', 'at', 't']