diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-07-08 21:22:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-08 21:22:19 +0100 |
| commit | 1e8f00ba123954a8306774361f33a47d125037c0 (patch) | |
| tree | 0ca214e7d7e562055516ddce166e35c17b0d2595 | |
| parent | 94000ab3b46c3e18f706e131c1211a9f1830dfc2 (diff) | |
| parent | 9924b55f4052cff9d96a6164a93549dfbfecd67f (diff) | |
| download | perlweeklychallenge-club-1e8f00ba123954a8306774361f33a47d125037c0.tar.gz perlweeklychallenge-club-1e8f00ba123954a8306774361f33a47d125037c0.tar.bz2 perlweeklychallenge-club-1e8f00ba123954a8306774361f33a47d125037c0.zip | |
Merge pull request #6405 from jo-37/contrib
Solutions to challenge 172
| -rwxr-xr-x | challenge-172/jo-37/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-172/jo-37/perl/ch-2.pl | 83 |
2 files changed, 145 insertions, 0 deletions
diff --git a/challenge-172/jo-37/perl/ch-1.pl b/challenge-172/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..328e1e36d5 --- /dev/null +++ b/challenge-172/jo-37/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Math::Prime::Util 'forpart'; +use List::Util 'uniqint'; +use experimental 'signatures'; + +our $examples; + +run_tests() if $examples; # does not return + +die <<EOS unless @ARGV == 2; +usage: $0 [-examples] [M N] + +-examples + run the examples from the challenge + +M N + Find all partitions of M into N prime parts having no duplicates. + +EOS + + +### Input and Output + +say "@$_" for prime_part(@ARGV); + + +### Implementation + +sub prime_part ($m, $n) { + my @part; + + # Find all prime partitions and filter out those containing + # duplicates. + forpart {push @part, [@_] if @_ == uniqint @_} $m, {n => $n, prime => 1}; + + wantarray ? @part : \@part; +} + + +### Examples and tests + +sub run_tests { + + is scalar(prime_part(18, 2)), + bag { + item bag {item 5; item 13; end}; + item bag {item 7; item 11; end}; + end; + }, 'example 1'; + + is scalar(prime_part(19, 3)), + bag { + item bag {item 3; item 5; item 11; end}; + end; + }, 'example 2'; + + done_testing; + exit; +} diff --git a/challenge-172/jo-37/perl/ch-2.pl b/challenge-172/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..f65eaee3c7 --- /dev/null +++ b/challenge-172/jo-37/perl/ch-2.pl @@ -0,0 +1,83 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Math::Utils qw(ceil floor); + +our $examples; + +run_tests() if $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [--] [N...] + +-examples + run some examples + +N... + Calculate the five-number summary over the given numbers. + +EOS + + +### Input and Output + +say "@{[fivenum(@ARGV)]}"; + + +### Implementation + +# According to Wiki (https://en.wikipedia.org/wiki/Quartile), there is +# no agreement about the method to calculate the quartiles of discrete +# distributions. For aesthetic reasons I prefer "Method 2" because the +# five-number summary for a distribution of five numbers consist of +# these five numbers. The characteristic of "Method 2" is the inclusion +# of the median in both, the upper and the lower part for the +# determination of the first and third quartile in case of odd-sized +# samples. +# We may take the value of a non-integer index of an array as the mean +# of the surrounding values. No weighting is required here, as all +# indices will be multiples of 1/2. +# If the sample size is odd, the median index is an integer and thus the +# median is naturally part of the lower and the upper half of the sample +# as in "Method 2". The 1/4 and 3/4 indices thus will give the correct +# index for the first and third quartile. Otherwise in the case of an +# even sample size, the median index is in the middle between two +# integers and the 1/4 and 3/4 indices are therefore off by ±1/4. +# +sub fivenum { + # numbers need to be sorted. + my @n = sort {$a <=> $b} @_; + + # non-integer index around the first quartile. + my $q = $#n / 4; + # Offset for the exact (non-integer) indices of the first + # and third quartile. + my $o = ($#n % 2) / 4; + # zeroth (min), first, second (median), third and fourth (max) + # quartile. Interpolated if necessary by taking the mean of the + # surrounding values for non-integer indices. + map +($n[floor $_] + $n[ceil $_]) / 2, + 0, $q - $o, $#n / 2, 3 * $q + $o, $#n; +} + + +### Examples and tests + +sub run_tests { + is [fivenum(0, 0, 1, 2, 63, 61, 27, 13)], + [0, 0.5, 7.5, 44, 63], 'example from Wiki "Five-number summary'; + + is [fivenum(6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49)], + [6, 25.5, 40, 42.5, 49], 'example 1 from Wiki "Quartile"'; + + + is [fivenum(7, 15, 36, 39, 40, 41)], + [7, 15, 37.5, 40, 41], 'example 2 from Wiki "Quartile"'; + + is [fivenum(3, 5, 8, 13, 21)], + [3, 5, 8, 13, 21], "five numbers"; + + done_testing; + exit; +} |
