Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How can one generate all possible permutations of r elements out of n where r<n?

by supriyoch_2008 (Scribe)
on Feb 26, 2013 at 11:50 UTC ( #1020674=perlquestion: print w/ replies, xml ) Need Help??
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!/(4-3)!=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.

Comment on How can one generate all possible permutations of r elements out of n where r<n?
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.
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.

      Hi BrowserUK,

      Thank you very much for the code. But I am sorry that I do not understand the code as it does not start with #!/usr/bin/perl, the code I am a little acquainted with.

      With regards.

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.

      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

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.

      Hi space_monk,

      Thanks for the suggestions. I have used Math::Combinatorics to solve the problem of combinations of elements.

      With kind regards.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020674]
Approved by tmharish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-12-20 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (97 votes), past polls