aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-30 15:18:13 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-30 15:18:13 +0100
commit4e47f9adafb6341b94ff0d1613258d40e6ff6ac6 (patch)
treea08faa157f8e8dc660404a67e0fdccb720d2818d
parent5040558d650c65c8c2cd9f32aab137b40b63ab54 (diff)
downloadperlweeklychallenge-club-4e47f9adafb6341b94ff0d1613258d40e6ff6ac6.tar.gz
perlweeklychallenge-club-4e47f9adafb6341b94ff0d1613258d40e6ff6ac6.tar.bz2
perlweeklychallenge-club-4e47f9adafb6341b94ff0d1613258d40e6ff6ac6.zip
Add Perl solution
-rw-r--r--challenge-170/paulo-custodio/perl/ch-1.pl54
-rw-r--r--challenge-170/paulo-custodio/perl/ch-2.pl92
-rw-r--r--challenge-170/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-170/paulo-custodio/t/test-2.yaml14
-rw-r--r--challenge-196/paulo-custodio/basic/ch-1.bas2
-rw-r--r--challenge-196/paulo-custodio/basic/ch-2.bas2
-rw-r--r--challenge-196/paulo-custodio/c/ch-1.c2
-rw-r--r--challenge-196/paulo-custodio/c/ch-2.c2
-rw-r--r--challenge-196/paulo-custodio/cpp/ch-1.cpp2
-rw-r--r--challenge-196/paulo-custodio/cpp/ch-2.cpp2
-rw-r--r--challenge-196/paulo-custodio/forth/ch-1.fs2
-rw-r--r--challenge-196/paulo-custodio/forth/ch-2.fs2
-rw-r--r--challenge-196/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-196/paulo-custodio/perl/ch-2.pl2
14 files changed, 175 insertions, 10 deletions
diff --git a/challenge-170/paulo-custodio/perl/ch-1.pl b/challenge-170/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..4d36966785
--- /dev/null
+++ b/challenge-170/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+
+# Challenge 170
+#
+# Task 1: Primorial Numbers
+# Submitted by: Mohammad S Anwar
+#
+# Write a script to generate first 10 Primorial Numbers.
+#
+# Primorial numbers are those formed by multiplying successive prime numbers.
+#
+#
+# For example,
+#
+# P(0) = 1 (1)
+# P(1) = 2 (1x2)
+# P(2) = 6 (1x2×3)
+# P(3) = 30 (1x2×3×5)
+# P(4) = 210 (1x2×3×5×7)
+
+use Modern::Perl;
+
+# https://en.wikipedia.org/wiki/Primality_test
+sub is_prime {
+ my($n) = @_;
+ return 1 if $n == 2 || $n == 3;
+ return 0 if $n <= 1 || $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 next_prime {
+ my($p) = @_;
+ $p++;
+ $p++ while !is_prime($p);
+ return $p;
+}
+
+sub primorial_numbers {
+ my($n) = @_;
+ my @primorial = (1);
+ my $p = 1;
+ while (@primorial < $n) {
+ $p = next_prime($p);
+ push @primorial, $primorial[-1] * $p;
+ }
+ return @primorial;
+}
+
+@ARGV==1 or die "usage: ch-1.pl n\n";
+my $n=shift;
+say join ", ", primorial_numbers($n);
diff --git a/challenge-170/paulo-custodio/perl/ch-2.pl b/challenge-170/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..dfff30bef8
--- /dev/null
+++ b/challenge-170/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,92 @@
+#!/usr/bin/perl
+
+# Challenge 170
+#
+# Task 2: Kronecker Product
+# Submitted by: Mohammad S Anwar
+#
+# You are given 2 matrices.
+#
+# Write a script to implement Kronecker Product on the given 2 matrices.
+#
+# For more information, please refer wikipedia page.
+#
+# For example,
+#
+# A = [ 1 2 ]
+# [ 3 4 ]
+#
+# B = [ 5 6 ]
+# [ 7 8 ]
+#
+# A x B = [ 1 x [ 5 6 ] 2 x [ 5 6 ] ]
+# [ [ 7 8 ] [ 7 8 ] ]
+# [ 3 x [ 5 6 ] 4 x [ 5 6 ] ]
+# [ [ 7 8 ] [ 7 8 ] ]
+#
+# = [ 1x5 1x6 2x5 2x6 ]
+# [ 1x7 1x8 2x7 2x8 ]
+# [ 3x5 3x6 4x5 4x6 ]
+# [ 3x7 3x8 4x7 4x8 ]
+#
+# = [ 5 6 10 12 ]
+# [ 7 8 14 16 ]
+# [ 15 18 20 24 ]
+# [ 21 24 28 32 ]
+
+use Modern::Perl;
+
+# current input line in $_
+sub parse_matrix {
+ my @a;
+ s/^\s*\w+\s*=\s*// or die "assignment expected: $_";
+ while (defined $_) {
+ /\s*\[([\s\d]+)\]\s*$/ or last;
+ my @row = split(' ', $1);
+ push @a, \@row;
+ $_=<>;
+ }
+ return @a;
+}
+
+sub parse_input {
+ $_ = "";
+ $_=<> while /^\s*$/;
+ my @a = parse_matrix();
+ $_=<> while /^\s*$/;
+ my @b = parse_matrix();
+ return \@a, \@b;
+}
+
+sub kronecker_product {
+ my($a, $b) = @_;
+ my $wa = @{$a->[0]}; my $ha = @$a;
+ my $wb = @{$b->[0]}; my $hb = @$a;
+ my @prod;
+ for my $ar (0..$ha-1) {
+ for my $ac (0..$wa-1) {
+ for my $br (0..$hb-1) {
+ for my $bc (0..$wb-1) {
+ my $tr = $ar*$hb + $br;
+ my $tc = $ac*$wb + $bc;
+ $prod[$tr] ||= [];
+ $prod[$tr][$tc] = $a->[$ar][$ac] * $b->[$br][$bc];
+ }
+ }
+ }
+ }
+ return @prod;
+}
+
+sub print_matrix {
+ my(@a) = @_;
+ for (@a) {
+ print "[ ";
+ for (@$_) {
+ printf("%2d ", $_);
+ }
+ print "]\n";
+ }
+}
+
+print_matrix(kronecker_product(parse_input()));
diff --git a/challenge-170/paulo-custodio/t/test-1.yaml b/challenge-170/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..e725034574
--- /dev/null
+++ b/challenge-170/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: 1, 2, 6, 30, 210, 2310, 30030, 510510, 9699690, 223092870
diff --git a/challenge-170/paulo-custodio/t/test-2.yaml b/challenge-170/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..0f5beb09d2
--- /dev/null
+++ b/challenge-170/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,14 @@
+- setup:
+ cleanup:
+ args:
+ input: |
+ |A = [ 1 2 ]
+ | [ 3 4 ]
+ |
+ |B = [ 5 6 ]
+ | [ 7 8 ]
+ output: |
+ |[ 5 6 10 12 ]
+ |[ 7 8 14 16 ]
+ |[ 15 18 20 24 ]
+ |[ 21 24 28 32 ]
diff --git a/challenge-196/paulo-custodio/basic/ch-1.bas b/challenge-196/paulo-custodio/basic/ch-1.bas
index d96c2d638a..504c7d5b7e 100644
--- a/challenge-196/paulo-custodio/basic/ch-1.bas
+++ b/challenge-196/paulo-custodio/basic/ch-1.bas
@@ -1,4 +1,4 @@
-' Challenge 197
+' Challenge 196
'
' Task 1: Pattern 132
' Submitted by: Mohammad S Anwar
diff --git a/challenge-196/paulo-custodio/basic/ch-2.bas b/challenge-196/paulo-custodio/basic/ch-2.bas
index b1fefc8c8a..21913f3d7e 100644
--- a/challenge-196/paulo-custodio/basic/ch-2.bas
+++ b/challenge-196/paulo-custodio/basic/ch-2.bas
@@ -1,4 +1,4 @@
-' Challenge 197
+' Challenge 196
'
' Task 2: Range List
' Submitted by: Mohammad S Anwar
diff --git a/challenge-196/paulo-custodio/c/ch-1.c b/challenge-196/paulo-custodio/c/ch-1.c
index 6445371db7..05d8d05187 100644
--- a/challenge-196/paulo-custodio/c/ch-1.c
+++ b/challenge-196/paulo-custodio/c/ch-1.c
@@ -1,5 +1,5 @@
/*
-Challenge 197
+Challenge 196
Task 1: Pattern 132
Submitted by: Mohammad S Anwar
diff --git a/challenge-196/paulo-custodio/c/ch-2.c b/challenge-196/paulo-custodio/c/ch-2.c
index 0f82a96990..b146b852ef 100644
--- a/challenge-196/paulo-custodio/c/ch-2.c
+++ b/challenge-196/paulo-custodio/c/ch-2.c
@@ -1,5 +1,5 @@
/*
-Challenge 197
+Challenge 196
Task 2: Range List
Submitted by: Mohammad S Anwar
diff --git a/challenge-196/paulo-custodio/cpp/ch-1.cpp b/challenge-196/paulo-custodio/cpp/ch-1.cpp
index 8c62d7d84e..3fa927d040 100644
--- a/challenge-196/paulo-custodio/cpp/ch-1.cpp
+++ b/challenge-196/paulo-custodio/cpp/ch-1.cpp
@@ -1,5 +1,5 @@
/*
-Challenge 197
+Challenge 196
Task 1: Pattern 132
Submitted by: Mohammad S Anwar
diff --git a/challenge-196/paulo-custodio/cpp/ch-2.cpp b/challenge-196/paulo-custodio/cpp/ch-2.cpp
index 04c5a3f0b1..870bef26f4 100644
--- a/challenge-196/paulo-custodio/cpp/ch-2.cpp
+++ b/challenge-196/paulo-custodio/cpp/ch-2.cpp
@@ -1,5 +1,5 @@
/*
-Challenge 197
+Challenge 196
Task 2: Range List
Submitted by: Mohammad S Anwar
diff --git a/challenge-196/paulo-custodio/forth/ch-1.fs b/challenge-196/paulo-custodio/forth/ch-1.fs
index 2cb27cc3f4..3c5c072e56 100644
--- a/challenge-196/paulo-custodio/forth/ch-1.fs
+++ b/challenge-196/paulo-custodio/forth/ch-1.fs
@@ -1,6 +1,6 @@
#! /usr/bin/env gforth
-\ Challenge 197
+\ Challenge 196
\
\ Task 1: Pattern 132
\ Submitted by: Mohammad S Anwar
diff --git a/challenge-196/paulo-custodio/forth/ch-2.fs b/challenge-196/paulo-custodio/forth/ch-2.fs
index 8af253fc45..6b93298219 100644
--- a/challenge-196/paulo-custodio/forth/ch-2.fs
+++ b/challenge-196/paulo-custodio/forth/ch-2.fs
@@ -1,6 +1,6 @@
#! /usr/bin/env gforth
-\ Challenge 197
+\ Challenge 196
\
\ Task 2: Range List
\ Submitted by: Mohammad S Anwar
diff --git a/challenge-196/paulo-custodio/perl/ch-1.pl b/challenge-196/paulo-custodio/perl/ch-1.pl
index d28eed71b1..23d5602a4f 100644
--- a/challenge-196/paulo-custodio/perl/ch-1.pl
+++ b/challenge-196/paulo-custodio/perl/ch-1.pl
@@ -1,6 +1,6 @@
#!/usr/bin/perl
-# Challenge 197
+# Challenge 196
#
# Task 1: Pattern 132
# Submitted by: Mohammad S Anwar
diff --git a/challenge-196/paulo-custodio/perl/ch-2.pl b/challenge-196/paulo-custodio/perl/ch-2.pl
index a5f5137546..9d0614c777 100644
--- a/challenge-196/paulo-custodio/perl/ch-2.pl
+++ b/challenge-196/paulo-custodio/perl/ch-2.pl
@@ -1,6 +1,6 @@
#!/usr/bin/perl
-# Challenge 197
+# Challenge 196
#
# Task 2: Range List
# Submitted by: Mohammad S Anwar