aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-09 17:24:50 +0000
committerGitHub <noreply@github.com>2021-12-09 17:24:50 +0000
commit0ef350ae0e8afeb9aa9dc5ce452927eeb30e53c5 (patch)
tree3a3ec6c5d4c19818e693d4cf740b9ef4b731e53e
parenta8669618382ec2b46997a575b945c1bedfcc0b9b (diff)
parentaa7acc68fa307f84ae7c5b2e36749fafb5f29948 (diff)
downloadperlweeklychallenge-club-0ef350ae0e8afeb9aa9dc5ce452927eeb30e53c5.tar.gz
perlweeklychallenge-club-0ef350ae0e8afeb9aa9dc5ce452927eeb30e53c5.tar.bz2
perlweeklychallenge-club-0ef350ae0e8afeb9aa9dc5ce452927eeb30e53c5.zip
Merge pull request #5353 from pjcs00/wk142
Committed week 142
-rw-r--r--challenge-142/peter-campbell-smith/blog.txt2
-rwxr-xr-xchallenge-142/peter-campbell-smith/perl/ch-1.pl55
-rwxr-xr-xchallenge-142/peter-campbell-smith/perl/ch-2.pl29
3 files changed, 86 insertions, 0 deletions
diff --git a/challenge-142/peter-campbell-smith/blog.txt b/challenge-142/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..8672118648
--- /dev/null
+++ b/challenge-142/peter-campbell-smith/blog.txt
@@ -0,0 +1,2 @@
+https://pjcs-pwc.blogspot.com/2021/12/clarity-versus-brevity.html
+
diff --git a/challenge-142/peter-campbell-smith/perl/ch-1.pl b/challenge-142/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..6a9294f3e4
--- /dev/null
+++ b/challenge-142/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2021-12-08
+# PWC 142 task 1
+
+use v5.20;
+use warnings;
+use strict;
+
+# You are given positive integers, $m and $n.
+# Write a script to find total count of divisors of $m having last digit $n.
+
+# Some thoughts on this challenge: https://pjcs-pwc.blogspot.com/2021/12/perl-weekly-challenge-141-task-1-reads.html
+
+my ($test, @tests, $m, $n, $string1, $string2, $divisor, $dividend, $count, $s);
+
+# inputs - pairs of $m and $n
+@tests = ([24, 2], [30, 5], [24, 7], [37, 0], [45, 5], [1048575, 5], [1048576, 2]);
+
+# loop over tests
+for $test (@tests) {
+ ($m, $n) = @$test;
+ say qq[\nInput: \$m = $m, \$n = $n];
+ $count = 0;
+ $string1 = $string2 = '';
+
+ # loop over potential divisors
+ for $divisor (1 .. $m) {
+ $dividend = $m / $divisor;
+
+ # if it is a divisor
+ if ($dividend eq int($dividend)) {
+ $string1 .= qq[$divisor, ]; # list of all divisors
+ if ($divisor % 10 == $n) {
+ $string2 .= qq[$divisor, ]; # list of divisors with last digit $n
+ $count ++;
+ }
+ }
+ }
+
+ # tidy the ends of the strings
+ for $s ($string1, $string2) {
+ $s =~ s|, (\d+), $| and $1|;
+ }
+
+ # say the answers
+ say qq[Output: $count];
+ say qq[The divisors of $m are $string1];
+ if ($string2) {
+ say qq[There are only $count divisors with last digit $n: $string2];
+ } else {
+ say qq[There are no divisors with last digit $n];
+ }
+
+}
diff --git a/challenge-142/peter-campbell-smith/perl/ch-2.pl b/challenge-142/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..5ad2ef999e
--- /dev/null
+++ b/challenge-142/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2021-12-06
+# PWC 142 task 2
+
+use v5.20;
+use warnings;
+use strict;
+
+# Implement Sleep Sort. In this sorting technique, we create different threads for
+# each individual element present in the array. The thread is then made to sleep
+# for an amount of time that is eproprotionate to value of the element for which
+# it was created.
+
+use Parallel::ForkManager;
+
+my @array = (4, 6, 42, 3, 38, 54, 1, 17);
+say qq[Sleep sort of: ] . join(' ', @array);
+
+my $pm = Parallel::ForkManager->new(scalar @array); # number of parallel processes
+for my $a (@array) {
+ my $pid = $pm->start; # if this is the parent create a child
+ next if $pid; # ... and don't do the rest ogf the loop
+ sleep $a / 2; # if this is a child, sleep for $a/2 seconds
+ say $a; # ... and print $a
+ $pm->finish; # ... and stop
+}
+
+$pm->wait_all_children;