Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing variables in every possible order 3

Status
Not open for further replies.

SelfLearner

Programmer
Oct 5, 2007
2
US
Hi All,

I'm new here, so forgive me if this has already been posted.

This is not necessarily a PERL question, but more of a logic question. I'm posting it here because ultimately I'll do it in Perl.

We have 3 strings.

$a="me"
$b="you"
$c="him"

The first question is, how do I get these strings to be printed in every possible order?

For 3 strings, the orders are pretty easy to determine:

a-b-c
a-c-b
b-c-a
b-a-c
c-a-b
c-b-a

But what algorithm would you use so the code is not necessarily restricted to 3 variables?

This is really a math question isn't it?!!

Thanks
 
By making use of recursion, that's quite straightforward: here is a first shot (should be completed with error handling, obtaining the list in a specific order...).
The function returns a (reference to) a list of permutations of the numbers [tt](0..n-1)[/tt] that are to be used as indices of an array of strings to print.
The use of references may be quite criptic for a beginner though.
Code:
my@strings=('me','you','him');
my$refallperms=permutations($#strings);
for$p(@{$refallperms}){for$q(@{$p}){print$strings[$q],' '}print"\n"}
print'Number of permutations:',scalar@{$refallperms};

sub permutations{
  my$n=$_[0];
  my($i,$j,@perm);
  return[[0,1],[1,0]]if$n==1;
  my$refbase=permutations($n-1);
  for($i=0;$i<@{$refbase};$i++){
    push@perm,[$n,@{$$refbase[$i]}];
    for($j=0;$j<$n-1;$j++){
      push@perm,[@{$$refbase[$i]}[0..$j],$n,@{$$refbase[$i]}[$j+1..$n-1]];
    }
    push@perm,[@{$$refbase[$i]},$n];
  }
  return\@perm;
}

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Thank you all for the replies.

Prex1, that is brilliant. I do understand most lines there but yeah it does feel a bit cryptic when I get to the 'j' loop!

I don't want to copy your work, so I'll go thru each line as many times as necessary to fully understand it all. Any explanation for lines or a simpler approach is greatly appreciated.

Thanks
 
Just a minor point/comment. The original list is generally not considered to be one of the possible permutations. A list of three strings should return 5 permutations.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
[tt]$refbase[/tt] is a reference to an array containing references to arrays; so:
[tt]@{$refbase}[/tt] is the main array (of references)
[tt]$$refbase[$i][/tt] is an element of the main array (a reference)
[tt]@{$$refbase[$i]}[/tt] is the subarray whose reference was obtained in the previous line
[tt]@{$$refbase[$i]}[0..$j][/tt] is a slice (a part) of the array obtained in the previous line

And Kevin, my name is Franco.

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top