Results 1 to 6 of 6
  1. #1
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default 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.

    Java 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
    			
       
      
     }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I'll give you the basic palindrome algorithm, you modify it for your assignment:
    Java 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.
    Last edited by m00nchile; 04-27-2010 at 06:03 AM.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    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.

    Java 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;
      }
      	
     }

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by Meta View Post
    Java 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-

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by m00nchile View Post
    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

Similar Threads

  1. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  2. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 01:52 PM
  3. Recursion
    By Mika in forum New To Java
    Replies: 5
    Last Post: 01-04-2009, 02:13 AM
  4. Please help with recursion
    By pheonix in forum New To Java
    Replies: 9
    Last Post: 12-27-2008, 12:41 PM
  5. recursion
    By kdeighan in forum New To Java
    Replies: 3
    Last Post: 01-25-2008, 10:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •