FAQ: Recursion vs. Iteration - Coding Throwdown - Taco Cat

This community-built FAQ covers the “Taco Cat” exercise from the lesson “Recursion vs. Iteration - Coding Throwdown”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Recursion: Python

FAQs on the exercise Taco Cat

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I found a bug in this lesson.

Whenever I run the code I wrote, it tells me I pass, and I did, but it shows the last print call to be True when it is actually False. Even the print calls I use to compare the two characters show they are DIFFERENT, it still evaluates to True when I compare them.

def is_palindrome(word):
  if len(word) < 2:
    return True
  print(word[0])
  print(word[-1])
  if word[0] != word[-1]:
      return False
  return is_palindrome(word[1:-1])


# test cases
print(is_palindrome("abba") == True) 
print(is_palindrome("gmcmg") == True)
print(is_palindrome("") == True)
print(is_palindrome("abcd") == False) # Prints True when it is False.
2 Likes

Ah, it’s checking if “abcd” == False if “abcd” == False then this is True. You want all Trues because it means that your code works when it is supposed to and catches the False when it is suppose to.

It’s checking if your function produces a False when “abcd” is passed in. If it does this will print True. If “abcd” produces a True then “abcd” does not == False and it would say False and your code is not doing something it’s supposed to.

:upside_down_face: Hope this made some sense.

3 Likes

Soon as I read your first 7 words, it hit me! I was thinking, for some idiotic reason, that the == False areas of the statements were showing me what it SHOULD be.

Thanks for slapping me in the face and waking me up! I swear, coding for 13 hours makes you get loopy and miss things. Thanks you!

3 Likes

Does the below solution is a good approach recursively? I just wrote it, it did pass all the cases but, I am doubtful… It was not same as the hint given.

def is_palindrome(my_string):
if len(my_string) > 1:
if my_string[0] != my_string[-1]:
return False
else:
return is_palindrome(my_string[1:-1])
return True

2 Likes

I have it done like this. It runs successfully and gives the required result.
My question is, what goes wrong in the code if I do not have return in the elif statement. Why is return a must there?

def is_palindrome(my_string): 
  if len(my_string)== 1 or len(my_string)==0: 
    return True 
  elif my_string[0]== my_string[-1]:  
    return(is_palindrome(my_string[1: -1]))
  else: 
    return False 
# test cases
print(is_palindrome("abba") == True)
print(is_palindrome("abcba") == True)
print(is_palindrome("") == True)
print(is_palindrome("abcd") == False)
1 Like

Because it is necessary in building the call stack. When the string is consumed (whittled down to nothing) the base case is reached and the call stack winds down.

Just throwing this on there after seeing the other discussions for this. Only came here after checking the lesson’s answer against mine and noticing the 4x True result which confused me at first like OP (this bit cleared up here already as the condition for the last one to be True is that the result is False)

What I am curious of, is that from my code below:

def is_palindrome(my_string):
  if len(my_string) <= 1:
    return True
  elif my_string[0] == my_string[-1]:
    return is_palindrome(my_string[1:-1])

Results:

True
True
True
False

So I got the last condition wrong, but the lesson still passed me on this. With the obvious correction to my code being the:

  else:
    return False

Being added, which then yielded all True.

Just thought id throw that out there as it still passes you for incomplete code.

Maybe not the best for readability but just wanted to throw in this one-liner.

is_palindrome = lambda input: len(input) < 2 or (input[0] == input[-1] and is_palindrome(input[1:-1]))
2 Likes

The “win condition” of this exercise is funny. I got unreasonably frustrated when I could not for the life of me figure out why the 4th call resulted in an output of “True”. I evaluated by hand, checked on my own machine, debugged using print statements… and still didn’t realize the call was

print(is_palindrome("abcd") == False)

rather than

print(is_palindrome("abcd"))

:sweat_smile:

Here is my code fwiw:

def is_palindrome(s):
    if len(s) < 2:
        return True
    else:
        return ((s[0] == s[-1]) & is_palindrome(s[1:-1]))
2 Likes