diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2022-07-09 12:52:52 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2022-07-09 12:52:52 +0200 |
| commit | 2266f8ae14363736e2b15e9c6cc6edb76d2eef4d (patch) | |
| tree | 82de3fb794ebb9dd6a6fdd18c9254dda00ffd147 | |
| parent | 691f04963d707495eae490ff7758740af5aa6d70 (diff) | |
| download | perlweeklychallenge-club-2266f8ae14363736e2b15e9c6cc6edb76d2eef4d.tar.gz perlweeklychallenge-club-2266f8ae14363736e2b15e9c6cc6edb76d2eef4d.tar.bz2 perlweeklychallenge-club-2266f8ae14363736e2b15e9c6cc6edb76d2eef4d.zip | |
feat(challenge-172/lubos-kolouch/perl/ch-{1,2}.pl): Challenge 172 Lubos Kolouch Perl
| -rw-r--r-- | challenge-172/lubos-kolouch/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-172/lubos-kolouch/perl/ch-2.pl | 41 |
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-172/lubos-kolouch/perl/ch-1.pl b/challenge-172/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..4acfc5e302 --- /dev/null +++ b/challenge-172/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,48 @@ +package main; +use strict; +use warnings; +use Math::Prime::Util qw/next_prime/; +use Math::Combinatorics; +use List::Util qw/sum/; +use Data::Dumper; + +sub generate_primes { + my $limit = shift; + + my @primes = (2); + + while ( $primes[-1] < $limit ) { + push @primes, next_prime( $primes[-1] ); + } + + return \@primes; +} + +sub find_prime_partitions { + my ( $m, $n ) = @_; + + # Create a generator of combinations + my $combinat = Math::Combinatorics->new( + count => $n, + data => generate_primes($m), + ); + + my @result; + + # Loop throug the combinator, exit if found a valid combination + while ( my @comb = $combinat->next_combination ) { + my $arr_sum = sum(@comb); + push @result, \@comb if $arr_sum == $m; + } + + warn Dumper \@result; + return \@result; +} + +use Test::More; + +# the tests fail most of the time as ->next_combination is returning the elements in unpredictable order +is_deeply( find_prime_partitions( 18, 2 ), [ [ 5, 13 ], [ 7, 11 ] ] ); +is_deeply( find_prime_partitions( 19, 3 ), [ [ 3, 5, 11 ] ] ); +done_testing; +1; diff --git a/challenge-172/lubos-kolouch/perl/ch-2.pl b/challenge-172/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..ee05585e9d --- /dev/null +++ b/challenge-172/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,41 @@ +package main; +use strict; +use warnings; +use Statistics::Descriptive; + +sub get_five_nums_summary { + my @what = @_; + + my $stat = Statistics::Descriptive::Full->new(); + $stat->add_data(@what); + + my $sum = 0; + + # min + $sum += $stat->quantile(0); + + #max + $sum += $stat->quantile(4); + + #median + my $median = $stat->quantile(2); + $sum += $median; + + # lower quartile + $stat->clear; + $stat->add_data( grep { $_ < $median } @what ); + $sum += $stat->quantile(2); + + # higher quartile + $stat->clear; + $stat->add_data( grep { $_ > $median } @what ); + $sum += $stat->quantile(2); + + return $sum; +} + +use Test::More; + +is( get_five_nums_summary( ( 0, 0, 1, 2, 63, 61, 27, 13 ) ), 115 ); +done_testing; +1; |
