Home arrow static arrow Java Programming [Archive] - recursion
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - recursion
This topic has 10 replies on 1 page.

Posts:357
Registered: 8/5/04
recursion  
Aug 7, 2004 8:39 AM



 
Can anyone please explain each step occuring in the following code when

its method is called

mystery("12345")

I guess first step will be
5+ 234 +1 // see the return statement below (i.e. 52341)

  public static String mystery(String s)  {  int length = s.length();     if (length == 1)         return s;     if (length == 2)         return s.substring(1) + s.substring(0, 1);      return s.substring(length - 1) +             mystery(s.substring(1, length - 1)) +            s.substring(0 , 1);         } 
 

Posts:357
Registered: 8/5/04
Re: recursion  
Aug 7, 2004 8:43 AM (reply 1 of 10)



 

I guess first step will be
5+ 234 +1 // see the return statement below (i.e.
52341)
Sorry, it will be

4 + 234 + 1 .... whats next...
 

Posts:4,906
Registered: 23/07/02
Re: recursion  
Aug 7, 2004 8:44 AM (reply 2 of 10)



 
Have you been taught how to use System.out.println(...) yet? If not, now would be a good time to learn.
 

Posts:357
Registered: 8/5/04
Re: recursion  
Aug 7, 2004 8:52 AM (reply 3 of 10)



 
This is what I get with system. .. but I can't get it how come the middle one

i.e. mystery(...) is 3 at first? shouldn't it be 234?

4+ 3+ 2
5-+432+ 1
4+ 3+ 2

Final:

54321

 

Posts:1,135
Registered: 1/16/04
Re: recursion  
Aug 7, 2004 9:17 AM (reply 4 of 10)



 
Interesting isn't it... Tell you what, here is your recursive code with a bunch of println in the method, look at the output and see if you can figure out the answer to your question...

[code]public class RecursiveString {
public static String mystery(String s){
int length = s.length();
System.out.println("First " + s );
if (length == 1){
System.out.println("if (length == 1) " + s);
return s;}
if (length == 2){
System.out.println("if (length == 2) " + s.substring(1) + s.substring(0, 1));
return s.substring(1) + s.substring(0, 1);}
System.out.println("Last " + s.substring(length - 1) + mystery(s.substring(1, length - 1)) + s.substring(0 , 1));
return s.substring(length - 1) + mystery(s.substring(1, length - 1)) + s.substring(0 , 1);
}
public static void main( String [] args ){
if ((args[0] == null)|(args.length <= 0)){
System.exit(0);
}
System.out.println(mystery(args[0]));
}
}[code]

I am sure that you will have one of those enlightening experiences...

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:179
Registered: 7/30/04
Re: recursion  
Aug 7, 2004 9:17 AM (reply 5 of 10)



 
That would depend on where you put that print statement.
 

Posts:1,135
Registered: 1/16/04
Re: recursion  
Aug 7, 2004 9:18 AM (reply 6 of 10)



 
Interesting isn't it... Tell you what, here is your recursive code with a bunch of println in the method, look at the output and see if you can figure out the answer to your question...

public class RecursiveString {	   public static String mystery(String s){  	   		  int length = s.length();	   		  System.out.println("First " + s );	   		  if (length == 1){	   		     System.out.println("if (length == 1) " + s);	   	  	  	 return s;}	   		  if (length == 2){	   		     System.out.println("if (length == 2) " + s.substring(1) + s.substring(0, 1));	   	  	  	 return s.substring(1) + s.substring(0, 1);}	   	  	  System.out.println("Last " + s.substring(length - 1) + mystery(s.substring(1, length - 1)) + s.substring(0 , 1));	   		  return s.substring(length - 1) + mystery(s.substring(1, length - 1)) + s.substring(0 , 1);	   }	   public static void main( String [] args ){	   		  if ((args[0] == null)|(args.length <= 0)){			  	 System.exit(0);	   		  }	   		  System.out.println(mystery(args[0]));	   }}


I am sure that you will have one of those enlightening experiences...

And of course, to squash the dramatic effect, I blunder on the [code] tags...
Go figure...

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:357
Registered: 8/5/04
Re: recursion  
Aug 7, 2004 9:30 AM (reply 7 of 10)



 
Thanks man!

But how did 234 become 432? thats confusing!
 

Posts:357
Registered: 8/5/04
Re: recursion  
Aug 7, 2004 9:32 AM (reply 8 of 10)



 
oops nevermind sorry ! hehe got it!
 

Posts:8,813
Registered: 10/4/00
Re: recursion  
Aug 7, 2004 9:54 AM (reply 9 of 10)



 
Change this
public class LearnJava {    public static void main(String[] arghs) {        Problem myProblem = new Problem("Recursion");        if (!myProblem.comprehend()) postToJavaForum(myProblem);    }}
to this
public class LearnJava {    public static void main(String[] arghs) {        Problem myProblem = new Problem("Recursion");        int count=0;        while (!myProblem.comprehend() && count++<10) Problem.think();        if (!myProblem.comprehend()) postToJavaForum(myProblem);    }}
 

Posts:37,103
Registered: 3/30/99
Re: recursion  
Aug 7, 2004 11:30 AM (reply 10 of 10)



 
Have you been taught how to use
System.out.println(...) yet? If not, now would be a
good time to learn.

Yeah, I've only told him about 2 or 3 times in different threads over the last couple of days, and I'm sure he was told by me and others numerous times before that in other threads.
 
This topic has 10 replies on 1 page.