This program should return True if it is possible to generate the given sequence x from a given list of segments seglist. After finding one possible solution it should stop.
I tried replacing and repositioning the return commands, but there's always a different problem coming up.
def valid_sequence(x, seglist):
if x in seglist:
return True
for i in seglist:
if x.startswith(i):
return valid_sequence(x[len(i):], seglist)
return False
This returns the correct Boolean for sequence 'abc'and seglist ['a', 'ab', 'bc', 'c'], ['a', 'b', 'c'], and ['ab', 'bc'], but not for seglist ['a', 'ab', 'c'] because obviously it's going through the 'a' variant but won't be successfull and stops before going through 'ab', which it should.
I ran it through pythontutor and I understood some of the problems, but I could not determine how to fix them.
How do I write it so that it continues with segment 'ab' after taking the unsuccessful path of 'a'? Maybe I'm missing something else too, given that I can't wrap my head around this recursion.
Is it even remotely possible this way, or do I have to take a totally different approach?