aboutsummaryrefslogtreecommitdiff
path: root/challenge-142
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-08 19:11:39 +0000
committerGitHub <noreply@github.com>2021-12-08 19:11:39 +0000
commit77ce3907b304b2a92aaf176a42442e9c1edc2fea (patch)
tree168f8179d2676b4b9b94f40f4e113c79a9a1c91a /challenge-142
parent3a7c55ceeb619fb9a60f62eb0d18bf9c2edc12e9 (diff)
parenta27c6d48e5242f79e0d696b157d67e8c71594046 (diff)
downloadperlweeklychallenge-club-77ce3907b304b2a92aaf176a42442e9c1edc2fea.tar.gz
perlweeklychallenge-club-77ce3907b304b2a92aaf176a42442e9c1edc2fea.tar.bz2
perlweeklychallenge-club-77ce3907b304b2a92aaf176a42442e9c1edc2fea.zip
Merge pull request #5349 from jo-37/contrib
Solutions to challenge 142
Diffstat (limited to 'challenge-142')
-rwxr-xr-xchallenge-142/jo-37/perl/ch-1.pl64
-rwxr-xr-xchallenge-142/jo-37/perl/ch-2.pl87
2 files changed, 151 insertions, 0 deletions
diff --git a/challenge-142/jo-37/perl/ch-1.pl b/challenge-142/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..fb16b8073d
--- /dev/null
+++ b/challenge-142/jo-37/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use Math::Prime::Util 'divisors';
+use experimental 'signatures';
+
+our ($tests, $examples, $base);
+$base //= 10;
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 2;
+usage: $0 [-examples] [-tests] [-base=B] [M N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-base=B
+ Use base B. Default: 10
+
+M N
+ count the divisors of M having N as last digit in base B.
+
+EOS
+
+
+### Input and Output
+
+say num_div_mod(@ARGV, $base);
+
+
+### Implementation
+
+# Generalizing the task by taking "the last digit" in a given base.
+sub num_div_mod ($m, $n, $base=10) {
+ scalar grep $_ % $base == $n, divisors $m
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is num_div_mod(24, 2), 2, 'example 1';
+ is num_div_mod(30, 5), 2, 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is num_div_mod(9, 1, 2), 3, 'odd divisors of 9';
+ is num_div_mod(8, 0, 2), 3, 'even divisors of 8';
+ is num_div_mod(24, 0, 1), 8, 'all divisors of 24';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-142/jo-37/perl/ch-2.pl b/challenge-142/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..cb1491bfd1
--- /dev/null
+++ b/challenge-142/jo-37/perl/ch-2.pl
@@ -0,0 +1,87 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use AnyEvent;
+use Coro;
+use Coro::AnyEvent;
+
+use experimental 'signatures';
+
+our ($tests, $examples, $verbose);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [-verbose] [I1 I2 ...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-verbose
+ enable trace output
+
+I1 I2 ...
+ nonnegative integers to be sorted
+
+EOS
+
+
+### Input and Output
+
+say "@{sleep_sort(\@ARGV)}";
+
+
+### Implementation
+
+# Coros, as cooperative threads, generally do not run in parallel, thus
+# no locking is required when accessing shared data. Using a semaphore
+# for flow control: by waiting for the "ready" semaphore to become
+# available, the main thread allows the async coros to be scheduled and
+# regains control when all have finished with the same call.
+# Note: The sleep function is event-driven and will not block the
+# overall processing.
+sub sleep_sort ($arr) {
+ # Collect sorted result here:
+ my @sorted;
+ # Will be unlocked when all threads have finished:
+ my $ready = Coro::Semaphore->new(1 - @$arr);
+ # Create threads.
+ async {
+ my ($ready, $sorted, $time) = @_;
+ die "time machine not implemented" if $time < 0;
+ say "sleep for $time s" if $verbose;
+ Coro::AnyEvent::sleep $time;
+ say "woke up after $time s" if $verbose;
+ push @$sorted, $time;
+ $ready->up;
+ } $ready, \@sorted, $_ for @$arr;
+ # Start processing and wait for async threads.
+ $ready->down;
+
+ \@sorted;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples";
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ is sleep_sort([3, 1, 2, 1]), [1, 1, 2, 3], 'simple test';
+
+ local $verbose;
+ is sleep_sort([2, (1) x 9999]),
+ array {item 9999 => 2; etc;}, 'check last element';
+ }
+
+ done_testing;
+ exit;
+}