diff options
| author | Simon Miner <simon.miner@gmail.com> | 2020-06-29 22:49:29 -0400 |
|---|---|---|
| committer | Simon Miner <simon.miner@gmail.com> | 2020-06-29 22:49:29 -0400 |
| commit | 70552fa3c55dd81add1362217a0c448505e4ba7b (patch) | |
| tree | 925defbc86eb68e969c0aac1e688b4c8fbd410ed /challenge-067 | |
| parent | 1eae9b72da9c927529e8f5f69cd499b824ae9023 (diff) | |
| download | perlweeklychallenge-club-70552fa3c55dd81add1362217a0c448505e4ba7b.tar.gz perlweeklychallenge-club-70552fa3c55dd81add1362217a0c448505e4ba7b.tar.bz2 perlweeklychallenge-club-70552fa3c55dd81add1362217a0c448505e4ba7b.zip | |
Add ch-1.pl for number combinations task
Diffstat (limited to 'challenge-067')
| -rwxr-xr-x | challenge-067/simon-miner/perl/ch-1.pl | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-067/simon-miner/perl/ch-1.pl b/challenge-067/simon-miner/perl/ch-1.pl new file mode 100755 index 0000000000..716e8df984 --- /dev/null +++ b/challenge-067/simon-miner/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; + +my ( $m, $n ) = @ARGV; +die "Max number $m is must be an integer.\n" if $m =~ m/\D/; +die "Combination length $n is must be an integer.\n" if $n =~ m/\D/; + +my @combinations = get_combinations( 1, $m, $n - 1 ); +print join( "\n", @combinations ) . "\n"; + +sub get_combinations { + my ( $min, $m, $n ) = @_; + + my @combinations = (); + if ( $n ) { + for my $i ( $min .. ( $m - 1 )) { + for my $combo ( get_combinations( $i + 1, $m, $n - 1 ) ) { + push( @combinations, $i . $combo ); + } + } + } else { + @combinations = ( $min .. $m ); + } + + return @combinations; +} |
