0

Need an explanation for this recursion code

Hello, So I have a code that will put an asterisk "*" in between letters that are the same, for example if you input pairStar("Harry") it will have "Har*ry" as an output But the problem is I don't understand how can the code move into the 2nd index and above on the word when there is only 0 and 1 in the code as an index, and also I don't understand why is it [1:] instead of just [1], explanations welcome. https://code.sololearn.com/czDCR8VI17TG/

31st Aug 2019, 1:18 PM
Steven Sim
Steven Sim - avatar
6 Respostas
+ 1
your function recursively checks the entire string 0==1 return and now Check 1 and 2 then 2 and 3 and it does this because of you using slicing [1:] says go all the way to the end of string. So it moves through the indexes each time calling the function on what is left of the string. compare h == a, return h, call func, compare a==r, return a, call func, compare r == r return r and *, call func, compare r == y, return y. no more index position in string [1:] harry
31st Aug 2019, 2:40 PM
James
James - avatar
+ 1
Thank you James
31st Aug 2019, 2:52 PM
Steven Sim
Steven Sim - avatar
0
Slicing. string [1:] say go from 1 to the end of the string indexes
31st Aug 2019, 1:29 PM
James
James - avatar
0
James How can the code detect both r's in Harry though, in the code it is stated only for 0 and 1 index, while both r is in the 2nd and 3rd index?
31st Aug 2019, 1:57 PM
Steven Sim
Steven Sim - avatar
0
I missed a few there but hopefully this helps sorry if I am confusing you more. I am a bit hungover and trying to form a complete thought
31st Aug 2019, 2:41 PM
James
James - avatar
0
Once it returns a position it continues on the next call so on the second time a is [0] and it compares to r at [1] returns and does it again [1:] the call inside doesn't take the string as a whole it takes it from its current step.
31st Aug 2019, 2:46 PM
James
James - avatar