From 70552fa3c55dd81add1362217a0c448505e4ba7b Mon Sep 17 00:00:00 2001 From: Simon Miner Date: Mon, 29 Jun 2020 22:49:29 -0400 Subject: Add ch-1.pl for number combinations task --- challenge-067/simon-miner/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 challenge-067/simon-miner/perl/ch-1.pl 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; +} -- cgit