Get all the combinatorial problems of k elements in an array of n elements

Source: Internet
Author: User
Tags scalar perl script

You can write loops, or you can use modules.

Baidu for a long time to find a blog http://blog.sina.com.cn/s/blog_4a0824490101f1kc.html detailed introduction of Algorithm::combinatorics

Inspired by this, I found math::combinatorics.

Because the previous blog introduced algorithm::combinatorics, so this blog about Math::combinatorics

Perl script

Use Math::combinatorics;

My @n = QW (a b c);
My $combinat = math::combinatorics->new (count = 2,data = [@n]);
Print "Combinations of 2 from:". Join ("", @n). " \ n ";
Print "------------------------". ("--" x scalar (@n)). " \ n ";
while (my @combo = $combinat->next_combination) {
Print join (", @combo)." \ n ";
}
print "\ n";

Print "Display the permutations:". Join ("", @n). " \ n ";
Print "------------------------". ("--" x scalar (@n)). " \ n ";
while (my @permu = $combinat->next_permutation) {
Print join (", @permu)." \ n ";
}

Results

Combinations of 2 from:a b C
------------------------------
A b
A C
b C

Display the Permutations:a b C
------------------------------
A b C
A C b
b A C
b c A
c A B
C B A

Get all the combinatorial problems of k elements in an array of n elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.