diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2021-02-01 12:47:12 -0600 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2021-02-01 12:47:12 -0600 |
| commit | aea3f33677516ffdaba7eb305e97d666d4ad759a (patch) | |
| tree | 83a33b41543168e01df4bfe000f0069d9df44e18 | |
| parent | ac60bddb13f96402c3026283fe223388ed54fc27 (diff) | |
| download | perlweeklychallenge-club-aea3f33677516ffdaba7eb305e97d666d4ad759a.tar.gz perlweeklychallenge-club-aea3f33677516ffdaba7eb305e97d666d4ad759a.tar.bz2 perlweeklychallenge-club-aea3f33677516ffdaba7eb305e97d666d4ad759a.zip | |
Solutions to challenge 98
| -rw-r--r-- | challenge-098/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-098/wlmb/perl/ch-1.pl | 54 | ||||
| -rwxr-xr-x | challenge-098/wlmb/perl/ch-2.pl | 15 |
3 files changed, 70 insertions, 0 deletions
diff --git a/challenge-098/wlmb/blog.txt b/challenge-098/wlmb/blog.txt new file mode 100644 index 0000000000..6a08f68483 --- /dev/null +++ b/challenge-098/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2021/02/01/PWC098/ diff --git a/challenge-098/wlmb/perl/ch-1.pl b/challenge-098/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..58a2463887 --- /dev/null +++ b/challenge-098/wlmb/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl +# Perl weekly challenge 097 +# Task 1: Read N characters +# +# See https:/wlmb.github.io/2021/02/01/PWC098/#task-1-read-n-characters +use warnings; +use strict; +use v5.12; +use Scalar::Util qw(looks_like_number); +use List::Util qw(all); + +sub readN { # find and call the appropriate reader with the appropriate argument + my $filename=shift; + my $number=shift; + return reader($filename)->($number); #get the appropriate reader to read +} + + +sub reader { #returns a reader, maybe creating it. + state %reader; #hash of readers, one per seen filename + my $filename=shift; + $reader{$filename} //=new_reader($filename); +} + +sub new_reader {#create a new reader + my $filename=shift; + open my $fh, "<", $filename or die "Couldn't open $filename: $!"; + my $line=""; #plays the role of a buffer + my $reader= sub { #This is the actual reader routine + my $number=shift; + while($number>length($line)){ #get enough characters + my $nextline=<$fh>; + last unless $nextline; + #I remove newlines. If not desired, comment the next line + chomp($nextline); + $line .= $nextline; + } + my $result=substr $line,0,$number; + (substr $line,0,$number)=''; + return $result; + } +} + +usage() unless @ARGV%2==0; +usage() unless all {looks_like_number($ARGV[$_]) && $ARGV[$_]>=1}, map {2*$_+1} 0..(@ARGV-1)/2; +my $res; # Don't print empty lines after file is consumed +say $res while(@ARGV and $res=readN(shift @ARGV, shift @ARGV)); +sub usage { + say <<'END'; + ./ch-1.pl file1 N1 file2 N2 ... + Reads N1 chars from file1, N2 from file2 and so on. + Filenames may repeat +END +} diff --git a/challenge-098/wlmb/perl/ch-2.pl b/challenge-098/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..1da3ea4401 --- /dev/null +++ b/challenge-098/wlmb/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +# Perl weekly challenge 098 +# Task 2: Search Insert Position +# +# See https:/wlmb.github.io/2021/02/01/PWC098/#task-2-search-insert-position +use warnings; +use strict; +use v5.12; +use List::Util qw(uniq); +use List::MoreUtils qw(first_index); + +my ($N, @N)=@ARGV; +say "Input: \@N=(", join(", ", @N), ") and \$N=$N\n", + "Output: ", + first_index {$_==$N} sort {$a<=>$b} uniq (@N, $N); |
