diff options
| author | Michael Manring <michael@manring> | 2022-07-04 23:11:36 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-07-04 23:32:56 +0700 |
| commit | 1228dc0588dbe4c3bdd5d25798edfc1debf3b638 (patch) | |
| tree | 5aa44064f512acbbc05e6d7b7e593b5b25050ecc | |
| parent | 6e05df3564d4a23c122a47473db190f870fe7ccf (diff) | |
| download | perlweeklychallenge-club-1228dc0588dbe4c3bdd5d25798edfc1debf3b638.tar.gz perlweeklychallenge-club-1228dc0588dbe4c3bdd5d25798edfc1debf3b638.tar.bz2 perlweeklychallenge-club-1228dc0588dbe4c3bdd5d25798edfc1debf3b638.zip | |
pwc172 solution in perl
| -rw-r--r-- | challenge-172/pokgopun/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-172/pokgopun/perl/ch-2.pl | 47 |
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-172/pokgopun/perl/ch-1.pl b/challenge-172/pokgopun/perl/ch-1.pl new file mode 100644 index 0000000000..b4ad616a31 --- /dev/null +++ b/challenge-172/pokgopun/perl/ch-1.pl @@ -0,0 +1,37 @@ +### /* https://theweeklychallenge.org/blog/perl-weekly-challenge-172/ +### +### Task 1: Prime Partition +### +### Submitted by: [50]Mohammad S Anwar +### __________________________________________________________________ +### +### You are given two positive integers, $m and $n. +### +### Write a script to find out the Prime Partition of the given number. No +### duplicates allowed. +### +### For example, +### Input: $m = 18, $n = 2 +### Output: 5, 13 or 7, 11 +### +### Input: $m = 19, $n = 3 +### Output: 3, 5, 11 +### +### */ +use strict; +use warnings; +use Math::Prime::Util qw/forprimes/; +use Math::Combinatorics; + +die "need two positive integers m and n\n" unless @ARGV==2 && join(" ",@ARGV[0,1]) =~ /^\d+ \d+$/; +my ($m, $n) = @ARGV; + +my @p; +forprimes { push @p, $_ } $m; + +my $c = Math::Combinatorics->new( count => $n, data => \@p ); +{ + last unless my @combo = $c->next_combination(); + printf("%s\n", join(", ",@combo)) if eval(join(" + ",@combo))==$m; + redo; +} diff --git a/challenge-172/pokgopun/perl/ch-2.pl b/challenge-172/pokgopun/perl/ch-2.pl new file mode 100644 index 0000000000..fe31c8bea7 --- /dev/null +++ b/challenge-172/pokgopun/perl/ch-2.pl @@ -0,0 +1,47 @@ +### /* https://theweeklychallenge.org/blog/perl-weekly-challenge-172/ +### +### Task 2: Five-number Summary +### +### Submitted by: [51]Mohammad S Anwar +### __________________________________________________________________ +### +### You are given an array of integers. +### +### Write a script to compute the five-number summary of the given set of +### integers. +### +### You can find the definition and example in the [52]wikipedia page. +### */ +### /* +### lower and upper quartile using Method 1 mentioned in https://en.wikipedia.org/wiki/Quartile#Discrete_distributions +### +### Computing methods +### Discrete distributions +### For discrete distributions, there is no universal agreement on selecting the quartile values.[3] +### +### Method 1 +### +### 1) Use the median to divide the ordered data set into two-halves. +### If there is an odd number of data points in the original ordered data set, do not include the median (the central value in the ordered list) in either half. +### If there is an even number of data points in the original ordered data set, split this data set exactly in half. +### +### 2) The lower quartile value is the median of the lower half of the data. The upper quartile value is the median of the upper half of the data. +### +### This rule is employed by the TI-83 calculator boxplot and "1-Var Stats" functions. +### */ +use strict; +use warnings; + +die "please provide integers to calculate five-number summary" unless @ARGV && join(" ",@ARGV) =~ /^\d+(\s\d+)*$/; +my @a = sort{$a <=> $b} @ARGV; + +printf "Input: (%s)\n", join(", ",@a); +my @fn = ($a[-1], @a==1 ? $a[0] : median(@a[int(@a/2)+@a%2..$#a]), median(@a), @a==1 ? $a[0] : median(@a[0..int(@a/2)-1]), $a[0]); +my $i = 5; +foreach (qw/sample_minimum lower_quartile median upper_quartile sample_maximum/){ + printf "%s: %s\n", $_, $fn[--$i]; +} + +sub median{ + return @_ % 2 ? $_[int(@_/2)] : ($_[@_/2-1] + $_[@_/2])/2; +} |
