Ask Question

write a recursive function def reverse (string) that computes the reverse of a string. For example, reverse ("flow") should return "wolf" Hint: reverse the substring starting at the second character, then add the first character at the end

+3
Answers (1)
  1. 16 May, 23:18
    0
    def reverse (s):

    if (len (s) >1):

    return reverse (s[1:]) + s[0]

    else:

    return s

    print reverse ("flow")

    Explanation:

    The reverse of a string is the reverse of the substring starting at the 2nd char, followed by the first char. Of course you always need a stop criterium to end the recursion. Only recurse if the string has at least 2 characters.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “write a recursive function def reverse (string) that computes the reverse of a string. For example, reverse ("flow") should return "wolf" ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers