# Watson Crick Recursion Help

• 04-27-2010, 04:18 AM
Meta
Watson Crick Recursion Help
My teacher gave me a program, which i have to add and edit to. I dont understand exactly how to do this. it looks like im supposed to determine if the amount of letters in a string is less than or equal to 1 at one point. how do i determine that, becuase if(pal <= 1) is not allowed it seems.

Code:

``` public boolean recursivePalindrome( String pal )  {   // ***** writes the body of this method *****   // Using recursion, determine if a String representing   // a DNA sequence is a Watson-Crick Complemented palindrome   // If it is, return true, otherwise return false   // We call the method animate inside the body of this method   // The call to animate is already coded below   animate( pal );                                                     //if 1 or less characters left, is a palindrome - 1st base case                                                    //if 1st character is complement (A-T, C-G) of last, continue recursion                                   //otherwise 2nd base case - not a palindrome                              }```
• 04-27-2010, 05:00 AM
m00nchile
I'll give you the basic palindrome algorithm, you modify it for your assignment:
Code:

```boolean isPalindrome(String pal) {   if(pal.length() <= 1)     return true;   if(pal.charAt(0) == pal.charAt(pal.length()-1))     return isPalidrome(pal.substring(1,pal.length()));   return false; }```
So, first you check if you have a string of length 1 or 0, that is the base case that means the given string is a palindrome. If the strings length is greater than 1, check if the first and last letters are the same. If they are, chop off the first and last letter and continue recursion. If the first and last letter are not the same, the string is not a palindrome, in that case return false.
• 04-27-2010, 05:34 AM
Meta
this is what i have. its getting the first if statement working right, but for my other one, it keeps coming up false, if it should be true.

Code:

``` public boolean recursivePalindrome( String pal )  {   animate( pal );   if (pal.length() <= 1)   {           return true;   }   if(pal.charAt(0) == pal.charAt(pal.length() - 1))   {           return recursivePalindrome(pal.substring(1, pal.length()));   }   else   {           return false;   }            }```
• 04-27-2010, 06:43 AM
gcalvin
Quote:

Originally Posted by Meta
Code:

`          return recursivePalindrome(pal.substring(1, pal.length()));`

This doesn't look right to me. The substring should cut off the first and last character of the string, and this seems to only cut off the first.

-Gary-
• 04-27-2010, 07:07 AM
m00nchile
Yeah, you're right, it should be pal.substring(1,pal.length()-1). The mind does strange things when you go 20 hours without sleep :D
• 04-27-2010, 07:33 AM
JosAH
Quote:

Originally Posted by m00nchile
Yeah, you're right, it should be pal.substring(1,pal.length()-1). The mind does strange things when you go 20 hours without sleep :D

Make that pal.length()-2 because you're chopping off 2 characters; no need to worry because those OBOEs (Off By One Errors) always bite me and hit me on a daily basis when I don't really pay attention ;-)

kind regards,

Jos