logo CodeStepByStep logo

is_reverse

Language/Type: Python recursion recursive programming

Write a recursive function is_reverse that accepts two strings as a parameter and returns true if the two strings contain the same sequence of characters as each other but in the opposite order (ignoring capitalization), and false otherwise. For example, the string "hello" backwards is "olleh", so a call of is_reverse("hello", "olleh") would return true. Since the function is case-insensitive, you would also get a true result from a call of is_reverse("Hello", "oLLEh"). The empty string, as well as any one-letter string, is considered to be its own reverse. The string could contain characters other than letters, such as numbers, spaces, or other punctuation; you should treat these like any other character. The key aspect is that the first string has the same sequence of characters as the second string, but in the opposite order, ignoring case. The table below shows more examples:

Call Value Returned
is_reverse("CSE143", "341esc") true
is_reverse("Madam", "MaDAm") true
is_reverse("Q", "Q") true
is_reverse("", "") true
is_reverse("e via n", "N aIv E") true
is_reverse("Go! Go", "OG !OG") true
is_reverse("Obama", "McCain") false
is_reverse("banana", "nanaba") false
is_reverse("hello!!", "olleh") false
is_reverse("", "x") false
is_reverse("madam I", "i m adam") false
is_reverse("ok", "oko") false

You are not allowed to construct any structured objects other than strings (no list, dictionary, etc.) and you may not use any loops to solve this problem; you must use recursion. If you like, you may declare other functions to help you solve this problem, subject to the previous rules.

Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.