1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Recursion Python

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    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')

    Continue reading...

Compartilhe esta Página