diff options
| -rw-r--r-- | challenge-142/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-142/dave-jacoby/node/ch-2.js | 19 | ||||
| -rw-r--r-- | challenge-142/dave-jacoby/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-142/dave-jacoby/perl/ch-2.pl | 25 |
4 files changed, 74 insertions, 0 deletions
diff --git a/challenge-142/dave-jacoby/blog.txt b/challenge-142/dave-jacoby/blog.txt new file mode 100644 index 0000000000..0de108d89a --- /dev/null +++ b/challenge-142/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2021/12/06/sleep-on-it-the-weekly-challenge-142.html diff --git a/challenge-142/dave-jacoby/node/ch-2.js b/challenge-142/dave-jacoby/node/ch-2.js new file mode 100644 index 0000000000..11ed1d62db --- /dev/null +++ b/challenge-142/dave-jacoby/node/ch-2.js @@ -0,0 +1,19 @@ +#!/usr/bin/env node +"use strict"; + +// let us make an unsorted array +let array = Array(10) + .fill() + .map((n, i) => 1 + i) + .map((value) => ({ value, sort: Math.random() })) + .sort((a, b) => a.sort - b.sort) + .map(({ value }) => value); +console.log(array.join(" ")); + +// here, we're helped by the fact that JS does things at the +// millisecond level, not the second level +for (let i of array) { + setTimeout(() => { + process.stdout.write(i + " "); + }, i); +} diff --git a/challenge-142/dave-jacoby/perl/ch-1.pl b/challenge-142/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..ec669aaa2b --- /dev/null +++ b/challenge-142/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say state postderef signatures }; +no warnings qw{ experimental }; + +my @examples; +push @examples, [ 24, 2 ]; +push @examples, [ 30, 5 ]; + +for my $e (@examples) { + my ( $m, $n ) = $e->@*; + my $o = divisor_last_digit( $m, $n ); + say <<"END"; + Input: \$m = $m, \$n = $n + Output: $o +END +} + +sub divisor_last_digit ( $m, $n ) { + my @divisors; + for my $i ( 1 .. $m ) { + if ( $m % $i == 0 ) { + push @divisors, $i; + } + } + return scalar grep { $n == substr $_, -1, 1 } @divisors; +} diff --git a/challenge-142/dave-jacoby/perl/ch-2.pl b/challenge-142/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..776d696811 --- /dev/null +++ b/challenge-142/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use Parallel::ForkManager; + +my @unsorted = sort { rand 1 <=> rand 1 } 1 .. 10; +my $pm = Parallel::ForkManager->new(20); + +print 'Unsorted: '; +say join ' ', @unsorted; +say ''; + +print 'Sorted: '; +LOOP: for my $i (@unsorted) { + my $pid = $pm->start and next LOOP; + sleep $i; + print qq{$i }; + $pm->finish; +} +$pm->wait_all_children; +say ''; |
