diff options
| author | Walt Mankowski <waltman@pobox.com> | 2020-06-30 20:02:42 -0400 |
|---|---|---|
| committer | Walt Mankowski <waltman@pobox.com> | 2020-06-30 20:02:42 -0400 |
| commit | 8aeb4115265e025381abf10665e92ee0d41411af (patch) | |
| tree | 9f8db569bf9a5051440b7367007a712ffbf40e96 /challenge-067 | |
| parent | aab273335bc065444359cf9765b2475d9501564c (diff) | |
| download | perlweeklychallenge-club-8aeb4115265e025381abf10665e92ee0d41411af.tar.gz perlweeklychallenge-club-8aeb4115265e025381abf10665e92ee0d41411af.tar.bz2 perlweeklychallenge-club-8aeb4115265e025381abf10665e92ee0d41411af.zip | |
Perl solution to challenge 67 task 1
Algorithm::Combinatorics is doing all the hard work here.
Diffstat (limited to 'challenge-067')
| -rw-r--r-- | challenge-067/walt-mankowski/perl/ch-1.pl | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-067/walt-mankowski/perl/ch-1.pl b/challenge-067/walt-mankowski/perl/ch-1.pl new file mode 100644 index 0000000000..4493b84f9e --- /dev/null +++ b/challenge-067/walt-mankowski/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); +use Algorithm::Combinatorics qw(combinations); + +# TASK #1 › Number Combinations +# Submitted by: Mohammad S Anwar +# +# You are given two integers $m and $n. Write a script print all +# possible combinations of $n numbers from the list 1 2 3 … $m. +# +# Every combination should be sorted i.e. [2,3] is valid combination +# but [3,2] is not. Example: +# +# Input: $m = 5, $n = 2 +# +# Output: [ [1,2], [1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,4], [3,5], [4,5] ] +# +# Solution: This is straightforward with combinations() from +# Algorithm::Combinatorics + +my ($m, $n) = @ARGV; + +my @data = 1..$m; +my @combs; +my $iter = combinations(\@data, $n); +while (my $p = $iter->next) { + push @combs, sprintf('[%s]', join(',', $p->@*)); +} +say '[ ', join(", ", @combs), ' ]'; |
