Question: Python code: PYTHON code: # Defining the recursive function def reverse(string): # Writing the base case if (len(string) == 1): # If length of string
Python code:
PYTHON code:
# Defining the recursive function def reverse(string): # Writing the base case if (len(string) == 1): # If length of string is 1 , then its reverse is itself. return string # Getting the first letter of string s = string[0] # Removing the first letter of string string = string[1:] # Calling the reverse function on remaining string string = reverse(string) # Appending the 's' to it string = string + s # returning the reversed string return string
# Testing the function print(reverse("pineapple")) print(reverse("cherimoya")) print(reverse("america")) print(reverse("level"))
Please add docstrings for every function:
Example:
Function: reverse( string ) Parameters: string - string to be reversed Outputs: None Returns: reversed string
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. For example, to reverse "flow": Step 1 - reverse "low" to "wol", then add the "f" at the end Step 2 - repeat Step 1 for "low" - thus the recursive call Use these test cases: pineapple cherimoya (do you know what it is? Best fruit in the world!!) america level
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
