aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-05 09:12:26 +0100
committerGitHub <noreply@github.com>2021-05-05 09:12:26 +0100
commit058d4cba239d431adadaa9924817d7017b47a140 (patch)
tree1e9a06ea7446a16890fe7f1d72181e5e389a1d13
parent5e66b581b9649e5bf461bc28bd391d25589e9258 (diff)
parentc59dd0f995e409ea342bec6cf33b77261d19e8fd (diff)
downloadperlweeklychallenge-club-058d4cba239d431adadaa9924817d7017b47a140.tar.gz
perlweeklychallenge-club-058d4cba239d431adadaa9924817d7017b47a140.tar.bz2
perlweeklychallenge-club-058d4cba239d431adadaa9924817d7017b47a140.zip
Merge pull request #4019 from wlmb/challenges
Add solutions to PWC 111
-rw-r--r--challenge-111/wlmb/blog.txt1
-rwxr-xr-xchallenge-111/wlmb/perl/ch-1.pl29
-rwxr-xr-xchallenge-111/wlmb/perl/ch-2.pl26
-rwxr-xr-xchallenge-111/wlmb/perl/ch-2a.pl23
4 files changed, 79 insertions, 0 deletions
diff --git a/challenge-111/wlmb/blog.txt b/challenge-111/wlmb/blog.txt
new file mode 100644
index 0000000000..1dce75d5e9
--- /dev/null
+++ b/challenge-111/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2021/05/03/PWC111/
diff --git a/challenge-111/wlmb/perl/ch-1.pl b/challenge-111/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..fd4b2da227
--- /dev/null
+++ b/challenge-111/wlmb/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 111
+# Task 1: Search matrix
+#
+# See https://wlmb.github.io/2021/05/03/PWC111/#task-1-search-matrix
+use strict;
+use warnings;
+use v5.12;
+use POSIX qw(floor);
+# Input matrix from STDIN (not ARGV) as space and newline separated columns and rows.
+# Assume it is ordered as stated above
+my @elements= map {chomp; (split ' ')} <STDIN>; # no need for organizing in rows
+say "Input: $_ Output: ", binary_search($_, @elements) foreach @ARGV;
+
+sub binary_search { # binary search an ordered list
+ my $number=shift @_;
+ my @array=@_;
+ return 0 if $number<$array[0] or $number > $array[-1]; # trivial case
+ my $low=0;
+ my $high=@array; #beyond highest number
+ while($low<$high){
+ my $mid=floor(($low+$high)/2);
+ return 1 if $array[$mid]==$number;
+ return 0 if $mid==$low;
+ $low=$mid if $number > $array[$mid];
+ $high=$mid if $number < $array[$mid];
+ }
+ return 0;
+}
diff --git a/challenge-111/wlmb/perl/ch-2.pl b/challenge-111/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..42388e8807
--- /dev/null
+++ b/challenge-111/wlmb/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 111
+# Task 2: Ordered letters
+#
+# See https://wlmb.github.io/2021/05/03/PWC111/#task-2-ordered-letters
+use strict;
+use warnings;
+use v5.12;
+use List::Util qw(all);
+my $word='';
+my $max_length=0;
+foreach(<>){
+ chomp;
+ $_=lc $_;
+ next unless m/^\w+$/; #Only allow word letters
+ next unless length>$max_length;
+ next unless sorted($_);
+ $word=$_;
+ $max_length=length;
+}
+say "\nword: $word length:\t$max_length";
+
+sub sorted {
+ my @chars=split '', shift;
+ return all {$chars[$_] le $chars[$_+1]} 0..$#chars-1;
+}
diff --git a/challenge-111/wlmb/perl/ch-2a.pl b/challenge-111/wlmb/perl/ch-2a.pl
new file mode 100755
index 0000000000..c2d1769390
--- /dev/null
+++ b/challenge-111/wlmb/perl/ch-2a.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 111
+# Task 2: Ordered letters
+#
+# See https://wlmb.github.io/2021/05/03/PWC111/#task-2-ordered-letters
+use strict;
+use warnings;
+use v5.12;
+use List::Util qw(all);
+my $length=shift @ARGV;
+foreach(<>){
+ chomp;
+ $_=lc $_;
+ next unless m/^\w+$/; #Only allow word letters
+ next unless length==$length;
+ next unless sorted($_);
+ say $_;
+}
+
+sub sorted {
+ my @chars=split '', shift;
+ return all {$chars[$_] le $chars[$_+1]} 0..$#chars-1;
+}