aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-11-16 11:05:42 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2023-11-16 11:05:42 +0000
commit464a39fa9370c41152f88dc06118540f70c84b2e (patch)
tree02c64daaeb3c7936608b6a1afbeff781489d557b
parentd20e7296170b997b7e690a58b79156f6c81f1cd2 (diff)
downloadperlweeklychallenge-club-464a39fa9370c41152f88dc06118540f70c84b2e.tar.gz
perlweeklychallenge-club-464a39fa9370c41152f88dc06118540f70c84b2e.tar.bz2
perlweeklychallenge-club-464a39fa9370c41152f88dc06118540f70c84b2e.zip
Add Perl solution
-rw-r--r--challenge-241/paulo-custodio/Makefile2
-rw-r--r--challenge-241/paulo-custodio/perl/ch-1.pl71
-rw-r--r--challenge-241/paulo-custodio/perl/ch-2.pl67
-rw-r--r--challenge-241/paulo-custodio/t/test-1.yaml10
-rw-r--r--challenge-241/paulo-custodio/t/test-2.yaml10
5 files changed, 160 insertions, 0 deletions
diff --git a/challenge-241/paulo-custodio/Makefile b/challenge-241/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-241/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-241/paulo-custodio/perl/ch-1.pl b/challenge-241/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..407c471494
--- /dev/null
+++ b/challenge-241/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+
+# Challenge 241
+#
+# Task 1: Arithmetic Triplets
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array (3 or more members) of integers in increasing order
+# and a positive integer.
+#
+# Write a script to find out the number of unique Arithmetic Triplets satisfying
+# the following rules:
+#
+# a) i < j < k
+# b) nums[j] - nums[i] == diff
+# c) nums[k] - nums[j] == diff
+#
+# Example 1
+#
+# Input: @nums = (0, 1, 4, 6, 7, 10)
+# $diff = 3
+# Output: 2
+#
+# Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
+# Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.
+#
+# Example 2
+#
+# Input: @nums = (4, 5, 6, 7, 8, 9)
+# $diff = 2
+# Output: 2
+#
+# (0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.
+# (1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.
+
+use Modern::Perl;
+
+# parse args
+sub usage { return "Usage: $0 -nums n n n ... -diff n\n"; }
+
+my(@nums, $diff);
+while (@ARGV) {
+ if ($ARGV[0] eq "-nums") {
+ shift;
+ while (@ARGV && $ARGV[0] !~ /^-/) {
+ push @nums, shift;
+ }
+ }
+ elsif ($ARGV[0] eq "-diff") {
+ shift;
+ $diff = shift;
+ }
+ else {
+ die usage();
+ }
+}
+if (!@nums || !$diff) { die usage(); }
+
+# compute
+my $count = 0;
+for my $i (0 .. $#nums-2) {
+ for my $j ($i+1 .. $#nums-1) {
+ for my $k ($j+1 .. $#nums) {
+ $count++ if ($nums[$j] - $nums[$i] == $diff &&
+ $nums[$k] - $nums[$j] == $diff);
+ }
+ }
+}
+
+# output
+say $count;
diff --git a/challenge-241/paulo-custodio/perl/ch-2.pl b/challenge-241/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..8c598ef885
--- /dev/null
+++ b/challenge-241/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/env perl
+
+# Challenge 241
+#
+# Task 2: Prime Order
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of unique positive integers greater than 2.
+#
+# Write a script to sort them in ascending order of the count of their prime
+# factors, tie-breaking by ascending value.
+# Example 1
+#
+# Input: @int = (11, 8, 27, 4)
+# Output: (11, 4, 8, 27))
+#
+# Prime factors of 11 => 11
+# Prime factors of 4 => 2, 2
+# Prime factors of 8 => 2, 2, 2
+# Prime factors of 27 => 3, 3, 3
+
+use Modern::Perl;
+
+sub is_prime {
+ my($n) = @_;
+ return 0 if $n <= 1;
+ return 1 if $n <= 3;
+ return 0 if ($n % 2)==0 || ($n % 3)==0;
+ for (my $i = 5; $i*$i <= $n; $i += 6) {
+ return 0 if ($n % $i)==0 || ($n % ($i+2))==0;
+ }
+ return 1;
+}
+
+sub prime_factors {
+ my($n) = @_;
+ my @factors;
+ my $p = 0;
+ while ($n > 1) {
+ do { $p++; } while (!is_prime($p));
+ while ($n % $p == 0) {
+ push @factors, $p;
+ $n /= $p;
+ }
+ }
+ return @factors;
+}
+
+# parse args
+@ARGV>0 or die "ch-2 n n...\n";
+my @nums = @ARGV;
+
+# process
+@nums = map { $_->[0] }
+ sort {
+ my(@fa) = @{$a->[1]};
+ my(@fb) = @{$b->[1]};
+ if (@fa != @fb) { return scalar(@fa) - scalar(@fb); }
+ for my $i (0 .. $#fa) {
+ if ($fa[$i] != $fb[$i]) { return $fa[$i] - $fb[$i]; }
+ }
+ return 0;
+ }
+ map {[$_, [prime_factors($_)]]} @nums;
+
+# output
+say "@nums";
diff --git a/challenge-241/paulo-custodio/t/test-1.yaml b/challenge-241/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..b4593c8ed1
--- /dev/null
+++ b/challenge-241/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: -nums 0 1 4 6 7 10 -diff 3
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: -nums 4 5 6 7 8 9 -diff 2
+ input:
+ output: 2
diff --git a/challenge-241/paulo-custodio/t/test-2.yaml b/challenge-241/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..2f50da7c69
--- /dev/null
+++ b/challenge-241/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 11 8 27 4
+ input:
+ output: 11 4 8 27
+- setup:
+ cleanup:
+ args: 11 27 8 4
+ input:
+ output: 11 4 8 27