We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Trouble with recursion
Page Index Toggle Pages: 1
Trouble with recursion (Read 1320 times)
Trouble with recursion
Apr 27th, 2009, 3:46pm
 
Having trouble with something real basic here and is getting frustrating.

mix("abc",2,"");

should produce output

aa ab ac ba bb bc ca cb cc

while    mix("ab",3,"");  would produce

aaa aab aba abb baa bab bba bbb

Cant wrap my mind around this for some reason, and because I know it should be easy, makes it more frustrating
Re: Trouble with recursion
Reply #1 - Apr 28th, 2009, 1:15am
 
Maybe not so easy, but interesting for sure. Some homework?
What the third parameter is for?
Just for fun, I will give a try.
Re: Trouble with recursion
Reply #2 - Apr 28th, 2009, 6:38am
 
oh, was using the third param as an empty set that gets filled untill its length is the same as int n.  was thinking along the lines of

void mix(String s, int n, String build){
 if  (string.length == n) {
   println(build);
   mix(String s, int n, "")
}

Im really not sure how to keep track of the words that get printed already so it doesnt keep repeating in an infinite loop as well as a few other things.
Re: Trouble with recursion
Reply #3 - Apr 28th, 2009, 11:33am
 
It looks to me like you are counting...

mix("abc",2,"");
aa ab ac ba bb bc ca cb cc

could be replaced with
mix("012",2,"");
00 01 02 10 11 12 20 21 22


The characters you supply changes the base.
The number example above is base 3.

Like any number system the it would roll over. In this case from 22 to 00.



In binary would be
mix("01",2,"");
00 01 10 11 *roll over* 00 01 10 11 00 rinse and repeat.
Re: Trouble with recursion
Reply #4 - Apr 28th, 2009, 12:21pm
 
not sure what you mean by rollover noah.  Also not sure if this changes anything but in my description I showed it returning the answers like aa bb cc ...  but im actually trying to have each one on its own line, if that changes anything like

aa
ab
ba
bb
...
Re: Trouble with recursion
Reply #5 - Apr 28th, 2009, 12:37pm
 
Good catch NoahBuddy.
I always avoid recursion when it is not needed.
Here is the result, using simple iterations.
Code:
void setup()
{
println(mix("abc", 2));
println(mix("ab", 3));
}

String[] mix(String digits, int numberLength)
{
int base = digits.length();
int sequenceLength = int(pow(base, numberLength));
println(sequenceLength);
String[] result = new String[sequenceLength];
String[] digitArray = digits.split("");
// println(digitArray);
for (int i = 0; i < sequenceLength; i++)
{
result[i] = MapDigits(i, numberLength, base, digitArray);
}

return result;
}

String MapDigits(int val, int numberLength, int base, String[] digitArray)
{
String r = "";
for (int i = 0; i < numberLength; i++)
{
int d = val % base;
val = val / base;
r = digitArray[d + 1] + r; // Small string, string concatenation OK here
}
return r;
}
Re: Trouble with recursion
Reply #6 - Apr 28th, 2009, 12:50pm
 
Eh... I think I tried to write two sentences there.

What I mean by rolling over is the same as when counting, the number resets and adds to the next collumn:

9 to 10
19 to 20
99 to 100
etc.
Re: Trouble with recursion
Reply #7 - Apr 28th, 2009, 2:48pm
 
yeah, recursive stuff is voodoo, i never feel in control of it. especially the stack usage.

that said, this wasn't that complex:

void mix(String alphabet, int n, String prefix) {
 if (n == 0) {
   // finished - prefix is entire string
   println(prefix);
   return;
 }
 for (int i = 0 ; i < alphabet.length() ; i++) {
   // add letter to what we have, reduce required length
   mix(alphabet, n - 1, prefix + alphabet.charAt(i));
 }
}

(if this is homework, don't use it if you don't understand it)
Re: Trouble with recursion
Reply #8 - Apr 28th, 2009, 2:48pm
 
Yes, it's pretty simple, tho I'm not inclined to do your homework for you.  Still, here's a hint:  If written recursively, the mix() routine as described only requires 4 statements (or perhaps 5, depending on if you'd count a certain compound statement as 1 or 2).   Good luck, cheers!
Page Index Toggle Pages: 1