__author__ = 'justin' def semordnilapWrapper(str1, str2): # A single-length string cannot be semordnilap if len(str1) == 1 or len(str2) == 1: return False # Equal strings cannot be semordnilap if str1 == str2: return False return semordnilap(str1, str2) def semordnilap(str1, str2): ''' str1: a string str2: a string returns: True if str1 and str2 are semordnilap; False otherwise. ''' # If the strings aren't the same length, not semordnilap if len(str1) != len(str2): return False # Base case: having worked the strings down to the last character - are they equal? if len(str1) == 1: return str1 == str2 # Recursive case: test the first char of str1 matches last char of str2 if str1[0] == str2[-1]: return semordnilap(str1[1:], str2[:-1]) else: return False print semordnilapWrapper('nametag', 'gateman') print semordnilapWrapper('dog', 'god') print semordnilapWrapper('gooble', 'gobble')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question