diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-02 08:13:30 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-02 08:13:30 +0000 |
| commit | 5718f33c209483ae5d307f3d9b95aed4fd1cc7cc (patch) | |
| tree | 0b3b34f5020b538decf808910bf54412da023d10 | |
| parent | 5165d640e47a53c7d7c0aa780c7faa17c03cb5f1 (diff) | |
| parent | 288e680102c2a81f7d014409010e3f196638bdb7 (diff) | |
| download | perlweeklychallenge-club-5718f33c209483ae5d307f3d9b95aed4fd1cc7cc.tar.gz perlweeklychallenge-club-5718f33c209483ae5d307f3d9b95aed4fd1cc7cc.tar.bz2 perlweeklychallenge-club-5718f33c209483ae5d307f3d9b95aed4fd1cc7cc.zip | |
Merge pull request #3445 from jacoby/master
Challenge 98!
| -rw-r--r-- | challenge-098/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-098/dave-jacoby/perl/ch-1a.pl | 49 | ||||
| -rw-r--r-- | challenge-098/dave-jacoby/perl/ch-1b.pl | 45 | ||||
| -rw-r--r-- | challenge-098/dave-jacoby/perl/ch-2.pl | 31 |
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; +} |
