Hi I am trying to find all possible substrings within a string
For example, if
string = 'abc'
I would like output to be
['', 'a', 'b', 'ab', 'c', 'ac', 'bc', 'abc']
This is a solution that works from
Getting all combinations of a string and its substrings (thank you @davidlowryduda)
def subs(string, ret=['']):
if len(string) == 0:
return ret
head = string[0]
tail = string[1:]
ret = ret + list(map(lambda x: x+head, ret)) #this is the line I have problem with
return subs(tail, ret)
and this is my code (I am trying not to use map or lambda function)
def addhead (ret, head):
for i in ret:
i += str(head)
return ret
def subs2(string, ret=['']):
if len(string) == 0:
return ret
head = string[0]
tail = string[1:]
ret = ret + addhead(ret,head) #this is the line that I changed
return subs(tail, ret)
However the output is now
['', '', 'b', 'b', 'c', 'c', 'bc', 'bc']
Does anyone knows what went wrong?
Thank you!