aboutsummaryrefslogtreecommitdiff
path: root/challenge-115
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-06-02 03:58:52 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-06-02 03:58:52 +0100
commit1efb5c3d8fd697fa448f8b92f64ddcf3bc1e6646 (patch)
treebd3f767f7bed557f275ee48867b4e22b3c24e910 /challenge-115
parentf517b444b33b21bb6be464b1296959f436cb8e75 (diff)
parent52052c2af912234604882c09c48fbd40f93e88c8 (diff)
downloadperlweeklychallenge-club-1efb5c3d8fd697fa448f8b92f64ddcf3bc1e6646.tar.gz
perlweeklychallenge-club-1efb5c3d8fd697fa448f8b92f64ddcf3bc1e6646.tar.bz2
perlweeklychallenge-club-1efb5c3d8fd697fa448f8b92f64ddcf3bc1e6646.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-115')
-rw-r--r--challenge-115/wlmb/blog.txt1
-rwxr-xr-xchallenge-115/wlmb/perl/ch-1.pl24
-rwxr-xr-xchallenge-115/wlmb/perl/ch-2.pl20
3 files changed, 45 insertions, 0 deletions
diff --git a/challenge-115/wlmb/blog.txt b/challenge-115/wlmb/blog.txt
new file mode 100644
index 0000000000..aee45b1687
--- /dev/null
+++ b/challenge-115/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2021/06/01/PWC115/
diff --git a/challenge-115/wlmb/perl/ch-1.pl b/challenge-115/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..a2accad0fd
--- /dev/null
+++ b/challenge-115/wlmb/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 115
+# Task 1: String Chain
+#
+# See https://wlmb.github.io/2021/06/01/PWC115/#task-1-string-chain
+use strict;
+use warnings;
+use v5.12;
+use List::Util qw(reduce);
+use PDL;
+
+my @strings=@ARGV;
+die "Usage ./ch-1.pl string1 [string2...]" unless @strings;
+my $C=zeroes(long,scalar(@strings), scalar(@strings)); #connectivity matrix
+map {my $f=$_;map {$C->slice("$f,$_").=follows($f, $_)}
+ (0..@strings-1)}(0..@strings-1);
+my $R=reduce {map{$_->diagonal(0,1).=0}($a,$b); $b x $a;}
+ ($C) x @strings;
+say "Input: ", join " ", @strings;
+say "Output: ", all($R->diagonal(0,1)>0);
+sub follows {
+ my ($from, $to)=map {$strings[$_]} @_;
+ return substr($from,-1,1) eq substr($to,0,1);
+}
diff --git a/challenge-115/wlmb/perl/ch-2.pl b/challenge-115/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..e58d9898cf
--- /dev/null
+++ b/challenge-115/wlmb/perl/ch-2.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 115
+# Task 2: Largest Multiple
+#
+# See https://wlmb.github.io/2021/06/01/PWC115/#task-2-largest-multiple
+use strict;
+use warnings;
+use v5.12;
+use List::MoreUtils qw(first_index);
+foreach(@ARGV){
+ die "Usage: ./ch-2.pl digits1 [digits2...]"
+ unless m/^\d+$/;
+ say "Input: $_";
+ my @digits=sort {$a<=>$b} split '', $_;
+ my $even_index=first_index {$_%2==0} @digits;
+ say("No even number may be constructed from input"), next
+ unless $even_index>=0;
+ my $last=splice @digits, $even_index,1;
+ say "Output: ", reverse(@digits), $last;
+}