aboutsummaryrefslogtreecommitdiff
path: root/challenge-023
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-01-22 23:38:26 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-01-22 23:38:26 +0000
commit15910994ab385dc68bc789d0c8443716dbefa347 (patch)
tree2dad03b2ed82ceb855d9f1590a0d06545e8ac718 /challenge-023
parent45a9178d4865a7289d6856d7756468f896a04e49 (diff)
downloadperlweeklychallenge-club-15910994ab385dc68bc789d0c8443716dbefa347.tar.gz
perlweeklychallenge-club-15910994ab385dc68bc789d0c8443716dbefa347.tar.bz2
perlweeklychallenge-club-15910994ab385dc68bc789d0c8443716dbefa347.zip
Add Perl solution to challenge 023
Diffstat (limited to 'challenge-023')
-rw-r--r--challenge-023/paulo-custodio/README1
-rw-r--r--challenge-023/paulo-custodio/perl/ch-1.pl36
-rw-r--r--challenge-023/paulo-custodio/perl/ch-2.pl62
-rw-r--r--challenge-023/paulo-custodio/test.pl32
4 files changed, 131 insertions, 0 deletions
diff --git a/challenge-023/paulo-custodio/README b/challenge-023/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-023/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-023/paulo-custodio/perl/ch-1.pl b/challenge-023/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..dd5d8d22a2
--- /dev/null
+++ b/challenge-023/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+# Challenge 023
+#
+# Task #1
+# Create a script that prints nth order forward difference series. You should
+# be a able to pass the list of numbers and order number as command line
+# parameters. Let me show you with an example.
+#
+# Suppose we have list (X) of numbers: 5, 9, 2, 8, 1, 6 and we would like to
+# create 1st order forward difference series (Y). So using the formula
+# Y(i) = X(i+1) - X(i), we get the following numbers:
+# (9-5), (2-9), (8-2), (1-8), (6-1).
+# In short, the final series would be: 4, -7, 6, -7, 5.
+# If you noticed, it has one less number than the original series.
+# Similarly you can carry on 2nd order forward difference series like:
+# (-7-4), (6+7), (-7-6), (5+7) => -11, 13, -13, 12.
+
+use strict;
+use warnings;
+use 5.030;
+
+my($n, @seq) = @ARGV;
+say join(", ", nth_forward_diff($n, @seq)), ".";
+
+
+sub forward_diff {
+ my(@in) = @_;
+ return map {$in[$_+1] - $in[$_]} 0..$#in-1;
+}
+
+sub nth_forward_diff {
+ my($n, @seq) = @_;
+ @seq = forward_diff(@seq) for 1 .. $n;
+ return @seq;
+}
diff --git a/challenge-023/paulo-custodio/perl/ch-2.pl b/challenge-023/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..e618898536
--- /dev/null
+++ b/challenge-023/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+
+# Challenge 023
+#
+# Task #2
+# Create a script that prints Prime Decomposition of a given number. The prime
+# decomposition of a number is defined as a list of prime numbers which when
+# all multiplied together, are equal to that number. For example, the Prime decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19.
+
+use strict;
+use warnings;
+use 5.030;
+
+my($n) = @ARGV;
+say join(", ", prime_decomposition($n)), ".";
+
+
+# check if number is prime
+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;
+}
+
+# next prime
+sub next_prime {
+ my($n) = @_;
+
+ return 2 if $n <= 1;
+ my $p = $n;
+ 1 while !is_prime(++$p);
+ return $p;
+}
+
+
+sub prime_decomposition {
+ my($n) = @_;
+ return ($n) if $n <= 2; # special cases
+
+ my @f; # factors
+ my $p = 2; # first prime
+ while ($n > 1) {
+ if ($n % $p == 0) {
+ push @f, $p;
+ $n /= $p;
+ }
+ else {
+ $p = next_prime($p);
+ }
+ }
+
+ return @f;
+}
diff --git a/challenge-023/paulo-custodio/test.pl b/challenge-023/paulo-custodio/test.pl
new file mode 100644
index 0000000000..70e613bf02
--- /dev/null
+++ b/challenge-023/paulo-custodio/test.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+use 5.030;
+use Path::Tiny;
+
+is capture("perl perl/ch-1.pl 1 5 9 2 8 1 6"), "4, -7, 6, -7, 5.\n";
+is capture("perl perl/ch-1.pl 2 5 9 2 8 1 6"), "-11, 13, -13, 12.\n";
+is capture("perl perl/ch-1.pl 1 4 -7 6 -7 5"), "-11, 13, -13, 12.\n";
+
+is capture("perl perl/ch-2.pl 1"), "1.\n";
+is capture("perl perl/ch-2.pl 2"), "2.\n";
+is capture("perl perl/ch-2.pl 3"), "3.\n";
+is capture("perl perl/ch-2.pl 4"), "2, 2.\n";
+is capture("perl perl/ch-2.pl 5"), "5.\n";
+is capture("perl perl/ch-2.pl 6"), "2, 3.\n";
+is capture("perl perl/ch-2.pl 7"), "7.\n";
+is capture("perl perl/ch-2.pl 8"), "2, 2, 2.\n";
+is capture("perl perl/ch-2.pl 9"), "3, 3.\n";
+is capture("perl perl/ch-2.pl 10"), "2, 5.\n";
+is capture("perl perl/ch-2.pl 228"), "2, 2, 3, 19.\n";
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
+}