aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-142/wlmb/blog.txt1
-rwxr-xr-xchallenge-142/wlmb/perl/ch-1.pl16
-rwxr-xr-xchallenge-142/wlmb/perl/ch-2.pl32
3 files changed, 49 insertions, 0 deletions
diff --git a/challenge-142/wlmb/blog.txt b/challenge-142/wlmb/blog.txt
new file mode 100644
index 0000000000..e1a69d4995
--- /dev/null
+++ b/challenge-142/wlmb/blog.txt
@@ -0,0 +1 @@
+https://wlmb.github.io/2021/12/09/PWC142/
diff --git a/challenge-142/wlmb/perl/ch-1.pl b/challenge-142/wlmb/perl/ch-1.pl
new file mode 100755
index 0000000000..24b1123a92
--- /dev/null
+++ b/challenge-142/wlmb/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 142
+# Task 1: divisor last digit
+#
+# See https://wlmb.github.io/2021/12/09/PWC142/#task-1-divisor-last-digit
+use v5.12;
+use warnings;
+
+say("Usage: ./ch-1.pl m n\nto count divisors of m ending in n"), exit unless @ARGV==2;
+my ($m,$n)=@ARGV;
+my $q=sqrt($m);
+my @divisors_ending_in_n=grep {$_=~/$n$/}
+ my @divisors=sort {$a<=>$b} map {1<$_<$q?($_, $m/$_):$_} grep {$m%$_==0}(1..$q);
+say "Input: m=$m n=$n,\nOutput: ", scalar(@divisors_ending_in_n),
+ "\nas the divisors of $m are: ", join(" ", @divisors),
+ "\nand those ending in $n are: ", join(" ", @divisors_ending_in_n), "\n";
diff --git a/challenge-142/wlmb/perl/ch-2.pl b/challenge-142/wlmb/perl/ch-2.pl
new file mode 100755
index 0000000000..80bf98719e
--- /dev/null
+++ b/challenge-142/wlmb/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 142
+# Task 2: sleep sort
+#
+# See https://wlmb.github.io/2021/12/09/PWC142/#task-2-sleep-sort
+use v5.12;
+use warnings;
+use Time::HiRes qw(sleep time);
+use POSIX ":sys_wait_h";
+my %value_for_kid;
+my @ordered;
+say("Usage: ./ch-2.pl scale a b c...\nto sort the numbers a b c...\n".
+ " Uses sleep sort. Sleep time is scale*number seconds"),
+ exit if @ARGV<=1;
+my $scale=shift @ARGV;
+foreach(@ARGV){
+ my $pid=fork;
+ die "couldn't fork" unless defined $pid;
+ if($pid==0){
+ sleep($scale*$_);
+ exit 0;
+ }
+ $value_for_kid{$pid}=$_;
+}
+
+while ((my $pid = waitpid(-1, 0))>0){
+ push @ordered, $value_for_kid{$pid};
+}
+
+say "\nInput: ", join ", ", @ARGV;
+say "Scale: $scale";
+say "Output: ", join ", ", @ordered;