def removedups(word):
if len(word)<=1:
return word
else:
if word[0]==word[1]:
return removedups(word[1:])
else:
return word[0]+removedups(word[1:])
print(removedups('aabbcc'))
I dont get how the recursion works for this case. My knowledge so far is:
1) it skips the base test
2) goes to the recursion call and returns 'abbcc', and then it starts over again:
3) the if statement in recursion call is false so you disregard it
4) The else statement is where i get confused when it says return word[0] +removedups(word[1:])
. Does it go the if statement and checks the word('bbcc')
return word[0]+removedups(word[1:])
word[0]+removedups(word[1:]) is returned only when the execution of removedups(word[1:]) is complete.
removedups('bbcc') returns 'bc'
So removedups('abbcc') returns 'a' + 'bc' i.e. 'abc'