aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2022-07-10 21:50:56 -0400
committerJaldhar H. Vyas <jaldhar@braincells.com>2022-07-10 21:50:56 -0400
commit0c3d710a62f0c993a49cc0f3fc5a17998a747518 (patch)
tree30d85e24f6955460fb0f6a9d2687205514e34b6a
parent54432b8360c5c63a4634657360b2d35f98931c0c (diff)
downloadperlweeklychallenge-club-0c3d710a62f0c993a49cc0f3fc5a17998a747518.tar.gz
perlweeklychallenge-club-0c3d710a62f0c993a49cc0f3fc5a17998a747518.tar.bz2
perlweeklychallenge-club-0c3d710a62f0c993a49cc0f3fc5a17998a747518.zip
Challenge 172 by Jaldhar H. Vyas.
-rw-r--r--challenge-172/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-172/jaldhar-h-vyas/perl/ch-1.pl87
-rwxr-xr-xchallenge-172/jaldhar-h-vyas/perl/ch-2.pl12
-rwxr-xr-xchallenge-172/jaldhar-h-vyas/raku/ch-1.raku20
-rwxr-xr-xchallenge-172/jaldhar-h-vyas/raku/ch-2.raku14
5 files changed, 134 insertions, 0 deletions
diff --git a/challenge-172/jaldhar-h-vyas/blog.txt b/challenge-172/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..e26a67b76a
--- /dev/null
+++ b/challenge-172/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2022/07/perl_weekly_challenge_week_172.html \ No newline at end of file
diff --git a/challenge-172/jaldhar-h-vyas/perl/ch-1.pl b/challenge-172/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..58c6eb17ef
--- /dev/null
+++ b/challenge-172/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,87 @@
+#!/usr/bin/perl
+use 5.030;
+use warnings;
+use English qw/ -no_match_vars /;
+
+sub combinations {
+ my @list = @{$_[0]};
+ my $length = $_[1];
+
+ if ($length <= 1) {
+ return map [$_], @list;
+ }
+
+ my @combos;
+
+ for (my $i = 0; $i + $length <= scalar @list; $i++) {
+ my $val = $list[$i];
+ my @rest = @list[$i + 1 .. $#list];
+ for my $c (combinations(\@rest, $length - 1)) {
+ push @combos, [$val, @{$c}] ;
+ }
+ }
+
+ return @combos;
+}
+
+sub isPrime {
+ my ($n) = @_;
+
+ if ($n < 2) {
+ return undef;
+ }
+
+ if ($n == 2) {
+ return 1;
+ }
+
+ for my $i (2 .. sqrt($n)) {
+ if ($n % $i == 0) {
+ return undef;
+ }
+ }
+
+ return 1;
+}
+
+sub sum {
+ my ($arr) = @_;
+ my $total = 0;
+
+ for my $elem (@{$arr}) {
+ $total += $elem;
+ }
+
+ return $total;
+}
+
+sub usage {
+print<<"-USAGE-";
+Usage:
+ $PROGRAM_NAME <m> <n>
+
+ <m> an integer to partition
+ <n> number of elements in partition
+-USAGE-
+ exit(0);
+}
+
+if (scalar @ARGV != 2) {
+ usage();
+}
+
+my ($m, $n) = @ARGV;
+
+my @primes = grep { isPrime($_) } 1 .. $m;
+my $count = 0;
+
+for my $combo (combinations(\@primes, $n)) {
+ if (sum($combo) == $m) {
+ say join q{, }, @{$combo};
+ $count++;
+ }
+}
+
+unless ($count) {
+ say "No such prime partition.";
+}
diff --git a/challenge-172/jaldhar-h-vyas/perl/ch-2.pl b/challenge-172/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..a46fba6b46
--- /dev/null
+++ b/challenge-172/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,12 @@
+#!/usr/bin/perl
+use 5.030;
+use warnings;
+
+my @nums = sort { $a <=> $b } @ARGV;
+my $e = scalar @nums;
+
+say "minimum: ", $nums[0];
+say "lower quartile: ", $e % 2 == 0 ? ($nums[$e / 4 - 1] + $nums[$e / 4]) / 2 : $nums[$e / 4];
+say "median: ", $e % 2 == 0 ? ($nums[$e / 2 - 1] + $nums[$e / 2]) / 2 : $nums[$e / 2];
+say "upper quartile: ", $e % 2 == 0 ? ($nums[$e / 4 * 3 - 1] + $nums[$e / 4 * 3]) / 2 : $nums[$e / 4 * 3];
+say "maximum: ", $nums[-1];
diff --git a/challenge-172/jaldhar-h-vyas/raku/ch-1.raku b/challenge-172/jaldhar-h-vyas/raku/ch-1.raku
new file mode 100755
index 0000000000..a39cba0720
--- /dev/null
+++ b/challenge-172/jaldhar-h-vyas/raku/ch-1.raku
@@ -0,0 +1,20 @@
+#!/usr/bin/raku
+
+sub MAIN(
+ Int $m, #= an integer to partition
+ Int $n #= number of elements in partition
+) {
+ my @primes = (1 .. $m).grep({ .is-prime });
+ my $count = 0;
+
+ for @primes.combinations($n) -> @combo {
+ if ([+] @combo) == $m {
+ @combo.join(q{, }).say;
+ $count++;
+ }
+ }
+
+ unless $count {
+ say "No such prime partition.";
+ }
+} \ No newline at end of file
diff --git a/challenge-172/jaldhar-h-vyas/raku/ch-2.raku b/challenge-172/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..10efa1f01e
--- /dev/null
+++ b/challenge-172/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,14 @@
+#!/usr/bin/raku
+
+sub MAIN(
+ *@args #= a series of numbers
+) {
+ my @nums = @args.sort;
+ my $e = @nums.elems;
+
+ say "minimum: ", @nums.min;
+ say "lower quartile: ", $e %% 2 ?? (@nums[$e / 4 - 1] + @nums[$e / 4]) / 2 !! @nums[$e / 4];
+ say "median: ", $e %% 2 ?? (@nums[$e / 2 - 1] + @nums[$e / 2]) / 2 !! @nums[$e / 2];
+ say "upper quartile: ", $e %% 2 ?? (@nums[$e / 4 * 3 - 1] + @nums[$e / 4 * 3]) / 2 !! @nums[$e / 4 * 3];
+ say "maximum: ", @nums.max;
+} \ No newline at end of file