I referred to several questions here about recursion but I am not able to understand how recursion works for this particular problem: Recursive program to get all combination of characters in a string in Python:
st= []
def combi(prefix, s):
if len(s)==0: return
else:
st.append(prefix+s[0])
''' printing values so that I can see what happens at each stage '''
print "s[0]=",s[0]
print "s[1:]=",s[1:]
print "prefix=",prefix
print "prefix+s[0]=",prefix+s[0]
print "st=",st
combi(prefix+s[0],s[1:])
combi(prefix,s[1:])
return st
print combi("",'abc')
I've made it print the values so that I can see what's happening. This is the output:
s[0]= a
s[1:]= bc
prefix=
prefix+s[0]= a
st= ['a']
s[0]= b
s[1:]= c
prefix= a
prefix+s[0]= ab
st= ['a', 'ab']
s[0]= c
s[1:]=
prefix= ab
prefix+s[0]= abc
st= ['a', 'ab', 'abc']
s[0]= c
s[1:]=
prefix= a ----> How did prefix become 'a' here. Shouldn't it be 'abc' ?
prefix+s[0]= ac
st= ['a', 'ab', 'abc', 'ac']
.........
.........
['a', 'ab', 'abc', 'ac', 'b', 'bc', 'c'] # final output
Full output: http://pastebin.com/Lg3pLGtP
As I've shown in the output, how did prefix become 'ab'?
I tried to visualize the recursive calls for the combi(prefix+s[0],s[1:]). Am I understanding it right?