aboutsummaryrefslogtreecommitdiff
path: root/challenge-067
diff options
context:
space:
mode:
authorSimon Miner <simon.miner@gmail.com>2020-06-29 22:49:29 -0400
committerSimon Miner <simon.miner@gmail.com>2020-06-29 22:49:29 -0400
commit70552fa3c55dd81add1362217a0c448505e4ba7b (patch)
tree925defbc86eb68e969c0aac1e688b4c8fbd410ed /challenge-067
parent1eae9b72da9c927529e8f5f69cd499b824ae9023 (diff)
downloadperlweeklychallenge-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-xchallenge-067/simon-miner/perl/ch-1.pl28
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;
+}