Results 1 to 6 of 6
Thread: Watson Crick Recursion Help
- 04-27-2010, 04:18 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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 }
- 04-27-2010, 05:00 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
I'll give you the basic palindrome algorithm, you modify it for your assignment:
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.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; }Last edited by m00nchile; 04-27-2010 at 05:03 AM.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 04-27-2010, 05:34 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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; } }
- 04-27-2010, 06:43 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 04-27-2010, 07:07 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 04-27-2010, 07:33 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 06:26 PM -
Recursion
By jachandru in forum New To JavaReplies: 1Last Post: 01-24-2009, 12:52 PM -
Recursion
By Mika in forum New To JavaReplies: 5Last Post: 01-04-2009, 01:13 AM -
Please help with recursion
By pheonix in forum New To JavaReplies: 9Last Post: 12-27-2008, 11:41 AM -
recursion
By kdeighan in forum New To JavaReplies: 3Last Post: 01-25-2008, 09:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks