aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2020-11-23 08:33:11 -0600
committerLuis Mochan <mochan@fis.unam.mx>2020-11-23 08:33:11 -0600
commit4f0144c47dd21aab3d330220fa78d7816a6aaaeb (patch)
tree2707c861b9f36ffc68031817525f257fbe73e555
parentcea83925f87e4bbe52ab8c1ce9a2e1b0f2d86611 (diff)
downloadperlweeklychallenge-club-4f0144c47dd21aab3d330220fa78d7816a6aaaeb.tar.gz
perlweeklychallenge-club-4f0144c47dd21aab3d330220fa78d7816a6aaaeb.tar.bz2
perlweeklychallenge-club-4f0144c47dd21aab3d330220fa78d7816a6aaaeb.zip
Add pdl solution to challenge 88
-rw-r--r--challenge-088/wlmb/README1
-rw-r--r--challenge-088/wlmb/perl/ch-1.pl19
-rw-r--r--challenge-088/wlmb/perl/ch-2.pl25
3 files changed, 45 insertions, 0 deletions
diff --git a/challenge-088/wlmb/README b/challenge-088/wlmb/README
new file mode 100644
index 0000000000..cfc9201ad7
--- /dev/null
+++ b/challenge-088/wlmb/README
@@ -0,0 +1 @@
+Solution by Luis Mochan wlmb
diff --git a/challenge-088/wlmb/perl/ch-1.pl b/challenge-088/wlmb/perl/ch-1.pl
new file mode 100644
index 0000000000..1abce9b7fc
--- /dev/null
+++ b/challenge-088/wlmb/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+# Perl Weekly Challenge 88 task 1 array of product
+# Given a matrix $N[i] produce a matrix $M[i]= of products of $N[j] excluding $N[i]
+use warnings;
+use strict;
+use feature qw(say);
+
+say join " ", "Example 1:", array_of_products(5,2,1,4,3);
+say join " ", "Example 2:", array_of_products(2,1,4,3);
+
+sub array_of_products {
+ use PDL; #use the perl data language
+ use PDL::NiceSlice;
+ my $input =pdl(@_); #input piddle (PDL array)
+ my $matrix=$input(:,*$input->dim(0))->copy; #replicate row to produce a matrix
+ $matrix->diagonal(0,1).=1; #replace diagonal by 1's
+ my $output=$matrix->prodover; #multiply elements row-wise
+ return $output->list; #convert to perl list
+}
diff --git a/challenge-088/wlmb/perl/ch-2.pl b/challenge-088/wlmb/perl/ch-2.pl
new file mode 100644
index 0000000000..18e95cead5
--- /dev/null
+++ b/challenge-088/wlmb/perl/ch-2.pl
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+# Perl Weekly Challenge 88 task 2 Spiral Matrix
+# Print elements of matrix along spiral
+use warnings;
+use strict;
+use feature qw(say);
+
+say join " ", "Example 1:", spiral_matrix([1,2,3],[4,5,6],[7,8,9]);
+say join " ", "Example 2:", spiral_matrix([1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]);
+
+sub spiral_matrix {
+ use PDL; #use the perl data language
+ use PDL::NiceSlice;
+ my $input =pdl(@_); #input piddle (PDL 2D array)
+ say $input;
+ my @output;
+ return if $input->dim(1)==0; # 0 rows no elements
+ while($input->dim(0) > 0){ #until no more columns
+ push @output, $input->(:,(0))->list; # walk through row
+ last if $input->dim(1)==1; # no more rows
+ $input=$input->(-1:0,1:-1) #reflect rows, remove one row
+ ->transpose; # rotate
+ }
+ return @output
+}