FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » Need hints recursively permuate a string

   
Author Topic: Need hints recursively permuate a string
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
Say I have an array Alpha in which is A-Z

I input N into the program and it grabs the first N letters.

And puts them into say Beta[ N-Nth ]

and heres a function

something Perm ( char [] );

so basically I need to code a function that will calc recursively every posible permutations of the array passed to it.

So, what kind of data type would I use to be able to return an array?

Somethng Perm ( char [] )
{


return Perm ( array )
}

Any hints in regards to logic and any help in regards to syntax?

IP: Logged | Report this post to a Moderator
TrapperKeeper
Member
Member # 7680

 - posted      Profile for TrapperKeeper   Email TrapperKeeper         Edit/Delete Post 
23.
Posts: 375 | Registered: Mar 2005  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
I thought it was 42.
IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
Blayne, do this:

make a spreadsheet with the letters ABCD each in a different cell in the top row.

Then manually go through and develop all the permutations.

Then go back and do it again, this time thinking about what you're doing and why you're doing it - particularly the order in which you're doing it.

Write that out in pseudocode and post it here.

I'm willing to help out with some logic, but you need to practice trying to do it on your own - before even trying to write any code.

Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
Dagonee
Member
Member # 5818

 - posted      Profile for Dagonee           Edit/Delete Post 
It might also be easier for you to do it with squares of paper cut out with letters on them. It depends on how you think best.
Posts: 26071 | Registered: Oct 2003  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
for ABC

ABC BAC CBA
ACB BCA CAB

6 permutations.

Since n = 3 if i were to use

code:
int Facts ( int n )
{
if ( n == 0 )

return 1;

else

return n * Facts( n - 1 );

}

This tells me the total number of permutations which I think would be useful for a loop.

Basically I know I need to make 1 function that will call itself.

as for ABC well theres 3 letters.

ABC

so take A

A then theres 2 letters, since we alrdy have a permutation with ABC just take 2 letters past A and switch them.

if no other permutations can be done increment to the next letter.

ABC

now were at b, so swap A and B.

BAC, gives us a new permutation

and then the next 2 numbers so BCA

rinse and repeat.

This is simple enough ild imagine an outer and inner loop would do Im just wondering how do it recursively.

IP: Logged | Report this post to a Moderator
fugu13
Member
Member # 2859

 - posted      Profile for fugu13   Email fugu13         Edit/Delete Post 
Think of it this way. Given a string of six characters, if you pick one, all possible permutations starting with that character are found by appending all possible permutations of the other five characters to that character.

So, all possible permutations of six characters can be found by iteratively picking each of the characters to be the first character, and finding all possible permutations of the other five (and appending them to the chosen first character. Think about that.

That makes me think of something interesting. It would be possible to do some really cool template generation to make permutation generation (up to any arbitrary length of string) extremely fast -- after all, you can think about any string as having an ordering, and then to generate permutations all you need are the permutations of the ordering, which can be easily pregenerated with templates; then its just a matter of sticking a string in what's essentially a completely unrolled permutation loop and getting the results.

Posts: 15770 | Registered: Dec 2001  |  IP: Logged | Report this post to a Moderator
mr_porteiro_head
Member
Member # 4644

 - posted      Profile for mr_porteiro_head   Email mr_porteiro_head         Edit/Delete Post 
Awesome.
Posts: 16551 | Registered: Feb 2003  |  IP: Logged | Report this post to a Moderator
xnera
Member
Member # 187

 - posted      Profile for xnera   Email xnera         Edit/Delete Post 
quote:
That makes me think of something interesting. It would be possible to do some really cool template generation to make permutation generation (up to any arbitrary length of string) extremely fast -- after all, you can think about any string as having an ordering, and then to generate permutations all you need are the permutations of the ordering, which can be easily pregenerated with templates; then its just a matter of sticking a string in what's essentially a completely unrolled permutation loop and getting the results.
*swoons*
Posts: 1805 | Registered: Jun 1999  |  IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2