Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

python - Add Character and Space in a string using recursion

I have one Recursive function in which Input is a string and in output, we are getting Double Character with Space. What is the logic of this?

def recursive(string):
     ...
     ...  

Input : "Hello"

Output : "HH ee ll ll oo"

question from:https://stackoverflow.com/questions/65948515/add-character-and-space-in-a-string-using-recursion

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
def recursive(string):
  if not string:
    return ''
  else:
    c = string[0]
    rest = string[1:]
    return c + c + ' ' + recursive(rest)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...