supriyoch_2008 has asked for the
wisdom of the Perl Monks concerning the following question:
Hi PerlMonks,
It might be a silly question. But I donot find a simple perl code to solve this problem. I have
an array like @a=qw/A B C D/; and I'm interested to get all possible permutations of say 3 elements out
of 4 in the array and their total numbers i.e. 4p3=4!/(43)!=24 permutations along with 4 other uniform permutations (i.e. AAA,BBB,
CCC,DDD). Thus total number of permutations will be 24+4=28. I need the permutations like ABC,ABD,BCD .. DDD and their
total numbers (28). I have gone through some posts but nowhere I find the answer to this type of basic question. May I request
the Perlmonks to provide suggestions for this problem? I am sure that I have put a very silly question.
Moreover, the code must work for permutations of 2 or 4 elements out of 4 i.e. 4p2 and 4p4 along with their uniform permutations
with little change in the code.
Re: How can one generate all possible permutations of r elements out of n where r<n? by BrowserUk (Pope) on Feb 26, 2013 at 12:29 UTC 
C:\test>perl MAlgorithm::Combinatorics=variations E"$i=variations( [
+'A'..'D'], 3 ); say @$_ while $_= $i>next"
ABC
ABD
ACB
ACD
ADB
ADC
BAC
BAD
BCA
BCD
BDA
BDC
CAB
CAD
CBA
CBD
CDA
CDB
DAB
DAC
DBA
DBC
DCA
DCB
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks  Silence betokens consent  Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
 [reply] [d/l] 

 [reply] 

 [reply] [d/l] 
Re: How can one generate all possible permutations of r elements out of n where r<n? by space_monk (Chaplain) on Feb 26, 2013 at 13:44 UTC 
A very fast look on CPAN reveals Math::Combinatorics
Despite the low version number, it looks as though it does what you want, and if it doesn't the source code is probably a good starting point to modify to do it.
IIRC a lot of advice round here advises Googling and checking CPAN, because it's probably been asked before :).
A Monk aims to give answers to those who have none, and to learn from those who know more.
 [reply] 

 [reply] 
Re: How can one generate all possible permutations of r elements out of n where r<n? by daxim (Chaplain) on Feb 26, 2013 at 12:31 UTC 
"uniform permutations" don't exist, at least not what you describe. Your number 4 is just the count of the elements of the input again, one does not need any algorithm to arrive at that conclusion.  [reply] 

Hi daxim,
Thanks for providing the information. It is true that uniform permutations don't exist. I used the term since I could not gather the appropriate term for permuations like AAA,TTT,GGG,CCC etc.
With regards
 [reply] 
Re: How can one generate all possible permutations of r elements out of n where r<n? by Anonymous Monk on Feb 26, 2013 at 12:07 UTC 
Several of your previous posts are on this style of topic, you "donot find a simple perl code", don't just search, try to write it yourself. Solve the problem with paper and pen, then convert to perl.  [reply] 

