diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-05 02:20:36 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-09-05 02:20:36 +0100 |
| commit | 29b84e358ea80e4929591f6ba51bcff4f340397b (patch) | |
| tree | 4b135e8d41728228e4f95cf29e01f184240c44b3 /challenge-076 | |
| parent | 742c8ccac08140e2fc0140b02ee06ad14bb825a9 (diff) | |
| download | perlweeklychallenge-club-29b84e358ea80e4929591f6ba51bcff4f340397b.tar.gz perlweeklychallenge-club-29b84e358ea80e4929591f6ba51bcff4f340397b.tar.bz2 perlweeklychallenge-club-29b84e358ea80e4929591f6ba51bcff4f340397b.zip | |
- Tidied up Perl solutions.
Diffstat (limited to 'challenge-076')
| -rwxr-xr-x | challenge-076/mohammad-anwar/perl/ch-1.pl | 23 | ||||
| -rwxr-xr-x | challenge-076/mohammad-anwar/perl/ch-1.t | 24 |
2 files changed, 23 insertions, 24 deletions
diff --git a/challenge-076/mohammad-anwar/perl/ch-1.pl b/challenge-076/mohammad-anwar/perl/ch-1.pl index ac66f3fd48..a2152c3528 100755 --- a/challenge-076/mohammad-anwar/perl/ch-1.pl +++ b/challenge-076/mohammad-anwar/perl/ch-1.pl @@ -15,35 +15,34 @@ use Algorithm::Combinatorics qw(combinations); my $SUM = $ARGV[0]; print "USAGE: perl $0 <positive_number>\n" and exit unless defined $SUM; -print prime_sum(find_prime_upto($SUM), $SUM); +_print(prime_sum(find_prime_upto($SUM), $SUM)); # # # METHODS +sub _print { + my ($prime_sum) = @_; + + foreach (@$prime_sum) { + print sprintf("%s\n", join ", ", @$_); + } +} + sub prime_sum { my ($primes, $sum) = @_; - print sprintf("Primes: %s\n", join(", ", @$primes)); my $prime_sum = []; foreach my $i (1 .. $sum) { last if ($i > @$primes); foreach my $comb (combinations($primes, $i)) { my $_sum = 0; $_sum += $_ for @$comb; - if ($_sum == $sum) { - if ((@$prime_sum == 0) || (@$prime_sum > @$comb)) { - $prime_sum = $comb; - } - } - - if (@$prime_sum) { - return sprintf("Prime Sum: %s\n", join ", ", @$prime_sum); - } + push @$prime_sum, $comb if ($_sum == $sum); } } - return "None found.\n"; + return $prime_sum; } sub find_prime_upto { diff --git a/challenge-076/mohammad-anwar/perl/ch-1.t b/challenge-076/mohammad-anwar/perl/ch-1.t index bd749b9f80..d1247b0001 100755 --- a/challenge-076/mohammad-anwar/perl/ch-1.t +++ b/challenge-076/mohammad-anwar/perl/ch-1.t @@ -11,10 +11,18 @@ use strict; use warnings; use Test::More; +use Test::Deep; use Algorithm::Combinatorics qw(combinations); -is(prime_sum(find_prime_upto(9), 9), "2, 7", "testing prime sum = 9"); -is(prime_sum(find_prime_upto(12), 12), "5, 7", "testing prime sum = 12"); +is_deeply(prime_sum(find_prime_upto(6), 6), + [], + "testing prime sum = 6"); +is_deeply(prime_sum(find_prime_upto(9), 9), + [[2, 7]], + "testing prime sum = 9"); +is_deeply(prime_sum(find_prime_upto(12), 12), + [[5, 7], [2, 3, 7]], + "testing prime sum = 12"); done_testing; @@ -31,19 +39,11 @@ sub prime_sum { foreach my $comb (combinations($primes, $i)) { my $_sum = 0; $_sum += $_ for @$comb; - if ($_sum == $sum) { - if ((@$prime_sum == 0) || (@$prime_sum > @$comb)) { - $prime_sum = $comb; - } - } - - if (@$prime_sum) { - return join ", ", @$prime_sum; - } + push @$prime_sum, $comb if ($_sum == $sum); } } - return 0; + return $prime_sum; } sub find_prime_upto { |
