1

First off, this IS homework, so I am not expecting any direct answers. I need to take two strings defined by a function (semordnilap(str1, str2)) and I need to see if they are equal when one is reversed. I was wondering if I can call these separately out of the function with semordnilap(str1[0:1) == semordnilap(str2[-1]) I tried this a few ways and I must not be thinking about it correctly, plus of course there is the kicker of trying to do this recursively. Any advise or direction would be helpful.

def semordnilap(str1, str2):
    '''
    str1: a string
    str2: a string

    returns: True if str1 and str2 are semordnilap
    False otherwise.
    '''
    if len(str1) != len(str2):
        return False
    if len(str1) <= 1 or len(str2) <= 1:
        return False
    if semordnilap(str1[0]) != semordnilap(str2[-1]):
        return False
    else:
        return True

This is what I have so far, getting error of TypeError: semordnilap() takes exactly 2 arguments (1 given)

kren470
  • 335
  • 1
  • 3
  • 13
Aoxx
  • 55
  • 1
  • 13
  • 1
    Can you show more of the code you have? For instance, how about a short and complete Python program? – Lasse V. Karlsen Nov 02 '13 at 20:22
  • To do this recursively (which you really shouldn't), you will probably want to compare the ends of both strings (`str1[0] == str1[-1]`) and *then* recurse into comparing the rest of them (`semordnilap(str1[1:], str1[:-1])`) – Lynn Nov 02 '13 at 20:29
  • @Lasse I added what I have so far, but it is far from complete or working. – Aoxx Nov 02 '13 at 20:29

2 Answers2

3

Given two strings str1 and str2, the easiest way to compare if one is equal to the reverse of the other is by using slicing:

str1 = 'racecar'
str2 = 'racecar'

str1 == str2[::-1]
Out[57]: True

Which is really just checking if str1 is a palindrome (i.e. a reverse of itself).

If you really want to use recursion, you also want to be using slicing: check if str1[0] == str2[-1], and then recursively call your function on str1[1:] and str2[:-1].

The [::-1] syntax is extended slicing syntax, which is valid for strings as well as lists and other sequences.

roippi
  • 25,533
  • 4
  • 48
  • 73
  • At the risk of sounding like a true amateur.... could you tell me "where" the slicing is happening? exp: "the [::] is what is doing the slicing and the -1 is the recur"... thank you – Aoxx Nov 02 '13 at 20:36
  • @Aoxx [::-1] is explained in this article: http://stackoverflow.com/questions/931092/reverse-a-string-in-python – kren470 Nov 02 '13 at 20:40
  • @Aoxx You should probably read [this](http://docs.python.org/2/tutorial/introduction.html#strings) part about strings of the python tutorial. – Bakuriu Nov 02 '13 at 20:40
  • @Bakuriu I will have to put some time aside to read that ( I get distracted easy) – Aoxx Nov 02 '13 at 20:58
2

To reverse a string you use 'this is a string'[::-1].

[::-1] Is slice notation which says include everything from the start to the end of the string but do it in reverse. 'abcdefghijk'[6:1:-2] outputs 'gec' because it goes from the 6th index (starting with 0) up to but not including the first index, in reverse steps of 2.

Read up more on slice notation:Explain Python's slice notation, http://docs.python.org/2.3/whatsnew/section-slices.html

def semordnilap(str1, str2):
    if str1 == str2[::-1]: return True
    else: return False

One way to do it recursively:

def semordnilap(str1, str2):
    if not (len(str1) or len(str2)): return True
    if not (len(str1) and len(str2)): return False
    if str1[0] != str2[-1]: return False
    return semordnilap(str1[1:], str2[:-1])

The first line checks if both strings are empty (0 evaluates to False, any other number is True). len(str1) returns the length as an integer.

Then it checks if only one of the strings is empty in which case they are not equal.

Then it checks if the first letter is the same as the last letter.

Then it repeats the process with the each string (minus the first letter of str1 and minus the last letter of str2). It goes until one of the base cases is reached. The base case is what is returned. So it will only return True when then first letter was equal to the last letter each round until both strings ran out of characters at the same time.

Community
  • 1
  • 1
Joel Green
  • 920
  • 9
  • 18
  • Thank you very much for the detail in the explanation, this helped me with comparing the two strings. – Aoxx Nov 02 '13 at 21:00