aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-098/dave-jacoby/blog.txt1
-rw-r--r--challenge-098/dave-jacoby/perl/ch-1a.pl49
-rw-r--r--challenge-098/dave-jacoby/perl/ch-1b.pl45
-rw-r--r--challenge-098/dave-jacoby/perl/ch-2.pl31
4 files changed, 126 insertions, 0 deletions
diff --git a/challenge-098/dave-jacoby/blog.txt b/challenge-098/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..832237b620
--- /dev/null
+++ b/challenge-098/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2021/02/01/various-positions-perl-weekly-challenge-98.html
diff --git a/challenge-098/dave-jacoby/perl/ch-1a.pl b/challenge-098/dave-jacoby/perl/ch-1a.pl
new file mode 100644
index 0000000000..28cb0eca12
--- /dev/null
+++ b/challenge-098/dave-jacoby/perl/ch-1a.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures state };
+no warnings qw{ experimental };
+
+use List::Util qw{ sum0 };
+
+use JSON;
+my $json = JSON->new->pretty->canonical;
+
+my $n = 4;
+my $flag = 0;
+my @inputs = qw{ twinkie input1.txt input2.txt };
+while ( $flag < @inputs ) {
+ state $flags;
+ for my $input (@inputs) {
+ my $output = readN( $input, $n );
+ do {
+ $flags->{$input} = 1;
+ $flag = sum0 values %$flags;
+ next;
+ } if $output eq '';
+ say qq{\t'$input'\t$n\t'$output'};
+ }
+}
+
+# returns empty string on failure cases, which include
+# * no file
+# * index beyond file length
+
+sub readN ( $file, $chars ) {
+ state $index;
+ $index->{$file} //= 0;
+ my $i = $index->{$file};
+ return '' unless -f $file;
+ return '' unless -r $file;
+ return '' if $i > -s $file;
+ my $output = '';
+
+ if ( open my $fh, '<', $file ) {
+ my $string = join '', <$fh>;
+ $output = substr $string, $i, $chars;
+ close $fh;
+ }
+ $index->{$file} += $chars;
+ return $output;
+}
diff --git a/challenge-098/dave-jacoby/perl/ch-1b.pl b/challenge-098/dave-jacoby/perl/ch-1b.pl
new file mode 100644
index 0000000000..fb06d584c4
--- /dev/null
+++ b/challenge-098/dave-jacoby/perl/ch-1b.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures state };
+no warnings qw{ experimental };
+
+use List::Util qw{ sum0 };
+
+use JSON;
+my $json = JSON->new->pretty->canonical;
+
+my $n = 4;
+my $flag = 0;
+my @inputs = qw{ twinkie input1.txt input2.txt };
+while ( $flag < @inputs ) {
+ state $flags;
+ for my $input (@inputs) {
+ my $output = readN( $input, $n );
+ do {
+ $flags->{$input} = 1;
+ $flag = sum0 values %$flags;
+ next;
+ } if $output eq '';
+ say qq{\t'$input'\t$n\t'$output'};
+ }
+}
+exit;
+
+# returns empty string on failure cases, which include
+# * no file
+# * index beyond file length
+
+sub readN ( $file, $chars ) {
+ state $fhs;
+ return '' unless -f $file;
+ return '' unless -r $file;
+ unless ( $fhs->{$file} ) { open $fhs->{$file}, '<', $file }
+
+ my $fh = $fhs->{$file};
+ my $output ;
+ read $fh, $output, $chars;
+ return $output;
+}
+
diff --git a/challenge-098/dave-jacoby/perl/ch-2.pl b/challenge-098/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..3a15e3b280
--- /dev/null
+++ b/challenge-098/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say signatures state };
+no warnings qw{ experimental };
+
+my @ns;
+push @ns, [ 3, [ 1, 2, 3, 4 ] ];
+push @ns, [ 6, [ 1, 3, 5, 7 ] ];
+push @ns, [ 10, [ 12, 15, 16, 18 ] ];
+push @ns, [ 19, [ 11, 13, 15, 17 ] ];
+
+for my $m (@ns) {
+ my $n = $m->[0];
+ my @n = $m->[1]->@*;
+ my $i = search_insert_position( $n, @n );
+ say qq{Input: \$n = $n};
+ say qq{ \@n = } . join ', ', @n;
+ say qq{Output: \$i = $i};
+ say '';
+}
+
+sub search_insert_position ( $n, @n ) {
+ my $i = 0;
+ while ( $i < @n ) {
+ return $i if $n <= $n[$i];
+ $i++;
+ }
+ return $i;
+}