4 Replies - 641 Views - Last Post: 06 March 2015 - 10:21 AM Rate Topic: -----

#1 Stefyboy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 15

Transform code to use recursion.

Posted 06 March 2015 - 10:00 AM

In order for this code to be qualified i need to figure out a way to make my code use recursion. I started off by using for loops to get the code up and working.


Here is the method that i wish to enable recursion


private static Map<Integer, String> GetPalindrome(String DNAString,Map<Integer, String> PalindromesMap) {
		int key = 1;
		for (int i = 1; i < DNAString.length() - 1; i++) {
			
			for (int start = i - 1, end = i + 1; start >= 0 && end < DNAString.length(); start--, end++) {
				
				if (DNAString.charAt(start) == DNAString.charAt(end)) {
					
					if (end - start + 1 >= 4 && end - start + 1 <= 12) {
						
						if (!PalindromesMap.containsValue(DNAString.substring(start, end + 1))) {
							System.out.println("key: "+key+" Index: "+start+" Length: "+DNAString.substring(start, end + 1).length());
							PalindromesMap.put(key,DNAString.substring(start, end + 1));
							key++;
						}
					}
				} else
					break;
			}
		}

		for (int i = 1; i < DNAString.length() - 1; i++) {
			
			for (int start = i, end = i + 1; start >= 0 && end < DNAString.length(); start--, end++) {
				
				if (DNAString.charAt(start) == DNAString.charAt(end)) {
					
					if (end - start + 1 >= 4 && end - start + 1 <= 12) {
						
						if (!PalindromesMap.containsValue(DNAString.substring(start, end + 1))) {
							System.out.println("key: "+key+" Index: "+start+" Length: "+DNAString.substring(start, end + 1).length());
							PalindromesMap.put(key,DNAString.substring(start, end + 1));
							key++;
						}
					}
				} else
					break;
			}
		}
		key=key-1;
		System.out.println("# of Palindromes: "+key);
		return PalindromesMap;
	}


This code gets RNA string from a file and searches for palindromes within that file and returns the index and length of the palindrome

Is This A Good Question/Topic? 0
  • +

Replies To: Transform code to use recursion.

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Transform code to use recursion.

Posted 06 March 2015 - 10:02 AM

Quote

Here is the method that i wish to enable recursion

What have you tried to do for the recursive work?
Was This Post Helpful? 0
  • +
  • -

#3 Stefyboy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 15

Re: Transform code to use recursion.

Posted 06 March 2015 - 10:09 AM

View Postmodi123_1, on 06 March 2015 - 10:02 AM, said:

Quote

Here is the method that i wish to enable recursion

What have you tried to do for the recursive work?

well i know that iam going to need to add a int start and int end to the parameters but im still clueless on how im gonna get the program to execute without running an unlimited amount of times
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Transform code to use recursion.

Posted 06 March 2015 - 10:12 AM

Let's step back and think critically. How would a loop know it is done? Something about "DNAString.length()", right? This means one recursion option is to send the 'current' index into the method call, and increment.. and send it back in. When that index is over the length stop calling the method. That's one way.
Was This Post Helpful? 0
  • +
  • -

#5 Stefyboy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-January 15

Re: Transform code to use recursion.

Posted 06 March 2015 - 10:21 AM

View Postmodi123_1, on 06 March 2015 - 10:12 AM, said:

Let's step back and think critically. How would a loop know it is done? Something about "DNAString.length()", right? This means one recursion option is to send the 'current' index into the method call, and increment.. and send it back in. When that index is over the length stop calling the method. That's one way.

ok so i was thinking that if i delete the for loops and then start the method of with

if (start <= DNAString.length())


while there will be a int start and an it end int the parameters.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1