diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-02-09 17:23:43 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-02-09 17:23:43 -0500 |
| commit | f73ff890f4e654931043943ea3b83b969279ac02 (patch) | |
| tree | 4e40a782e10bba7384a43f85f1227a3a51eab526 | |
| parent | fe8bf82ab0300914ba58aa60d84e16ddbf7a921d (diff) | |
| parent | a78c2abd77e625f86cc67cb71e0661147e910fd9 (diff) | |
| download | perlweeklychallenge-club-f73ff890f4e654931043943ea3b83b969279ac02.tar.gz perlweeklychallenge-club-f73ff890f4e654931043943ea3b83b969279ac02.tar.bz2 perlweeklychallenge-club-f73ff890f4e654931043943ea3b83b969279ac02.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
23 files changed, 2146 insertions, 1864 deletions
diff --git a/challenge-098/alexander-pankoff/perl/ch-1.pl b/challenge-098/alexander-pankoff/perl/ch-1.pl new file mode 100755 index 0000000000..19294205aa --- /dev/null +++ b/challenge-098/alexander-pankoff/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use v5.20; +use utf8; +use strict; +use warnings; +use feature qw(say signatures); +no warnings 'experimental::signatures'; + +{ + my ( $FILE, @numbers ) = @ARGV; + say readN( $FILE, $_ ) for @numbers; +} + +sub readN ( $file, $chars ) { + state $filehandles = {}; + + my $fh; + if ( $filehandles->{$file} ) { + $fh = $filehandles->{$file}; + } + else { + open( $fh, '<', $file ); + $fh->binmode( ':utf8' ); + $filehandles->{$file} = $fh; + } + + my $out; + while ( $chars-- && !$fh->eof ) { + $out .= $fh->getc; + } + + return $out; +} diff --git a/challenge-098/alexander-pankoff/perl/ch-2.pl b/challenge-098/alexander-pankoff/perl/ch-2.pl new file mode 100755 index 0000000000..6031dfaebf --- /dev/null +++ b/challenge-098/alexander-pankoff/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl +use v5.20; +use utf8; +use strict; +use warnings; +use feature qw(say signatures); +no warnings 'experimental::signatures'; + +use List::Util qw(first); + +{ + my @N = @ARGV; + my $N = pop @N; + + say join( " ", '@N:', @N ); + say join( " ", '$N:', $N ); + my ( $index, @new_N ) = search_insert_position( $N, @N ); + + my $human_index = $index + 1; + + say @new_N == @N + ? "$human_index since the target $N is in the array at the index $human_index." + : "$human_index since the target $N is missing and should be placed at the index $human_index." +} + +sub search_insert_position ( $target, @xs ) { + my $index = first_index( sub($x) { $x >= $target }, @xs ); + + if ( !$index ) { + return ( $#xs + 1, @xs, $target ); + } + elsif ( $xs[$index] && $xs[$index] == $target ) { + return ( $index, @xs ); + } + else { + return ( $index, @xs[ 0 .. $index - 1 ], $target, @xs[ $index .. $#xs ] ); + } +} + +sub first_index ( $cond, @xs ) { + first { $cond->( $xs[$_] ) } 0 .. $#xs; +} diff --git a/challenge-098/bob-lied/README b/challenge-098/bob-lied/README index be9398e9a3..07a4221a5d 100644 --- a/challenge-098/bob-lied/README +++ b/challenge-098/bob-lied/README @@ -1,3 +1,3 @@ -Solutions to weekly challenge 83 by Bob Lied. +Solutions to weekly challenge 98 by Bob Lied. -https://perlweeklychallenge.org/blog/perl-weekly-challenge-083/ +https://perlweeklychallenge.org/blog/perl-weekly-challenge-098/ diff --git a/challenge-098/bob-lied/perl/ch-1.pl b/challenge-098/bob-lied/perl/ch-1.pl new file mode 100755 index 0000000000..59dfafcfe8 --- /dev/null +++ b/challenge-098/bob-lied/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl +#============================================================================= +# Copyright (c) 2021, Bob Lied +#============================================================================= +# Perl Weekly Challenge 098 Challenge 1 +# You are given file $FILE. +# Create subroutine readN($FILE, $number) returns the first n-characters and +# moves the pointer to the (n+1)th character. +# Example: +# Input: Suppose the file (input.txt) contains "1234567890" +# Output: +# print readN("input.txt", 4); # returns "1234" +# print readN("input.txt", 4); # returns "5678" +# print readN("input.txt", 4); # returns "90" +#============================================================================= + +use strict; +use warnings; +use v5.20; + +use experimental qw/signatures /; + +sub Usage { "Usage: $0 filename N" }; + +my $FILE = shift; +my $N = shift; + +die Usage() unless $FILE; +die Usage() unless $N; +die "Need positive N" if ( $N <= 0 ); + +# Cache open file handles per file. The file handle will +# keep track of the position in the file, advancing each time +# that we call read(). +my %fileToFH; + +sub readN($s, $n) +{ + my $fh; + if ( exists $fileToFH{$s} ) + { + $fh = $fileToFH{$s}; + } + elsif ( open($fh, "<:utf8", $s) ) + { + $fileToFH{$s} = $fh; + } + else + { + die "Invalid filename $s ($!)"; + } + my $howmany = read($fh, my $bytes, $n); + return $bytes; +} +binmode(STDOUT, "utf8"); +say readN($FILE, $N); +say readN($FILE, $N); +say readN($FILE, $N); diff --git a/challenge-098/jaldhar-h-vyas/blog.txt b/challenge-098/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..88437d4900 --- /dev/null +++ b/challenge-098/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2021/02/perl_weekly_challenge_week_98.html diff --git a/challenge-098/jaldhar-h-vyas/input.txt b/challenge-098/jaldhar-h-vyas/input.txt new file mode 100644 index 0000000000..6a537b5b36 --- /dev/null +++ b/challenge-098/jaldhar-h-vyas/input.txt @@ -0,0 +1 @@ +1234567890
\ No newline at end of file diff --git a/challenge-098/jaldhar-h-vyas/perl/ch-1.pl b/challenge-098/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..ab82a8e5e7 --- /dev/null +++ b/challenge-098/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw / -no_match_vars /; + +sub readN { + my ($filename, $number) = @_; + my $buffer; + state $fh = undef; + + if (!defined $fh) { + open $fh, '<', $filename or die "$OS_ERROR\n"; + } + + read $fh, $buffer, $number or die "$OS_ERROR\n"; + + return $buffer; +} + +say readN('input.txt', 4); +say readN('input.txt', 4); +say readN('input.txt', 4); diff --git a/challenge-098/jaldhar-h-vyas/perl/ch-2.pl b/challenge-098/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..d2785f19d7 --- /dev/null +++ b/challenge-098/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw / -no_match_vars /; + +sub usage { + print <<"-USAGE-"; + $PROGRAM_NAME [<N> ...] + + [<N> ...] a series of atleast 2 distinct integers. The last element will be used as a target to search in the previous elements. +-USAGE- + exit 0; +} + +if (scalar @ARGV < 2) { + usage(); +} + +my $N = pop @ARGV; +my $pos = scalar @ARGV; + +for my $i (0 .. scalar @ARGV - 1) { + if ($ARGV[$i] >= $N) { + $pos = $i; + last; + } +} + +say $pos;
\ No newline at end of file diff --git a/challenge-098/jaldhar-h-vyas/raku/ch-1.raku b/challenge-098/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..9db5d0fa5c --- /dev/null +++ b/challenge-098/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,21 @@ +#!/usr/bin/raku + +sub readN(Str $filename, Int $number) { + state IO::Handle $fn = Nil; + + try { + unless $fn { + $fn = $filename.IO.open(:r); + } + + return $fn.readchars($number); + } + + die $!; +} + +sub MAIN() { + say readN('input.txt', 4); + say readN('input.txt', 4); + say readN('input.txt', 4); +}
\ No newline at end of file diff --git a/challenge-098/jaldhar-h-vyas/raku/ch-2.raku b/challenge-098/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..b96823fe58 --- /dev/null +++ b/challenge-098/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,19 @@ +#!/usr/bin/raku + +sub MAIN( + *@N #= a series of atleast 2 distinct integers. The last element will be + #= used as a target to search in the previous elements. + where {@_.elems > 1 } +) { + my $N = @N.pop; + my $pos = @N.elems; + + for 0 ..^ @N.elems -> $i { + if @N[$i] >= $N { + $pos = $i; + last; + } + } + + say $pos; +}
\ No newline at end of file diff --git a/stats/pwc-current.json b/stats/pwc-current.json index b268b91c9c..a4efee3002 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,31 +1,229 @@ { - "tooltip" : { - "followPointer" : 1, - "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", - "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>" + "plotOptions" : { + "series" : { + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + }, + "borderWidth" : 0 + } }, "chart" : { "type" : "column" }, - "yAxis" : { - "title" : { - "text" : "Total Solutions" + "series" : [ + { + "name" : "Perl Weekly Challenge - 098", + "colorByPoint" : 1, + "data" : [ + { + "y" : 3, + "name" : "Aaron Smith", + "drilldown" : "Aaron Smith" + }, + { + "y" : 2, + "name" : "Abigail", + "drilldown" : "Abigail" + }, + { + "y" : 4, + "name" : "Adam Russell", + "drilldown" : "Adam Russell" + }, + { + "drilldown" : "Alexander Pankoff", + "y" : 2, + "name" : "Alexander Pankoff" + }, + { + "y" : 2, + "name" : "Andinus", + "drilldown" : "Andinus" + }, + { + "name" : "Arne Sommer", + "y" : 5, + "drilldown" : "Arne Sommer" + }, + { + "y" : 4, + "name" : "Athanasius", + "drilldown" : "Athanasius" + }, + { + "y" : 1, + "name" : "Bob Lied", + "drilldown" : "Bob Lied" + }, + { + "y" : 1, + "name" : "Cheok-Yin Fung", + "drilldown" : "Cheok-Yin Fung" + }, + { + "drilldown" : "Colin Crain", + "name" : "Colin Crain", + "y" : 5 + }, + { + "drilldown" : "Cristina Heredia", + "name" : "Cristina Heredia", + "y" : 2 + }, + { + "y" : 2, + "name" : "Dave Cross", + "drilldown" : "Dave Cross" + }, + { + "drilldown" : "Dave Jacoby", + "name" : "Dave Jacoby", + "y" : 3 + }, + { + "drilldown" : "Duncan C. White", + "y" : 2, + "name" : "Duncan C. White" + }, + { + "name" : "E. Choroba", + "y" : 2, + "drilldown" : "E. Choroba" + }, + { + "drilldown" : "Flavio Poletti", + "name" : "Flavio Poletti", + "y" : 4 + }, + { + "name" : "Gustavo Chaves", + "y" : 2, + "drilldown" : "Gustavo Chaves" + }, + { + "drilldown" : "Jaldhar H. Vyas", + "y" : 5, + "name" : "Jaldhar H. Vyas" + }, + { + "drilldown" : "James Smith", + "name" : "James Smith", + "y" : 2 + }, + { + "name" : "Jan Krnavek", + "y" : 2, + "drilldown" : "Jan Krnavek" + }, + { + "y" : 2, + "name" : "Joan Mimosinnet", + "drilldown" : "Joan Mimosinnet" + }, + { + "drilldown" : "Jorg Sommrey", + "name" : "Jorg Sommrey", + "y" : 2 + }, + { + "drilldown" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", + "y" : 4 + }, + { + "drilldown" : "Lubos Kolouch", + "name" : "Lubos Kolouch", + "y" : 2 + }, + { + "name" : "Luca Ferrari", + "y" : 4, + "drilldown" : "Luca Ferrari" + }, + { + "y" : 2, + "name" : "Mark Anderson", + "drilldown" : "Mark Anderson" + }, + { + "drilldown" : "Markus Holzer", + "name" : "Markus Holzer", + "y" : 2 + }, + { + "drilldown" : "Niels van Dijke", + "name" : "Niels van Dijke", + "y" : 2 + }, + { + "drilldown" : "Nuno Vieira", + "y" : 2, + "name" : "Nuno Vieira" + }, + { + "drilldown" : "Paulo Custodio", + "y" : 2, + "name" : "Paulo Custodio" + }, + { + "drilldown" : "Pete Houston", + "y" : 2, + "name" : "Pete Houston" + }, + { + "drilldown" : "Philip Hood", + "y" : 2, + "name" : "Philip Hood" + }, + { + "drilldown" : "Roger Bell_West", + "y" : 5, + "name" : "Roger Bell_West" + }, + { + "name" : "Simon Green", + "y" : 3, + "drilldown" : "Simon Green" + }, + { + "name" : "Simon Proctor", + "y" : 2, + "drilldown" : "Simon Proctor" + }, + { + "drilldown" : "Stuart Little", + "y" : 4, + "name" : "Stuart Little" + }, + { + "drilldown" : "Ulrich Rieke", + "y" : 3, + "name" : "Ulrich Rieke" + }, + { + "drilldown" : "W. Luis Mochan", + "y" : 3, + "name" : "W. Luis Mochan" + }, + { + "drilldown" : "Wanderdoc", + "name" : "Wanderdoc", + "y" : 2 + } + ] } + ], + "title" : { + "text" : "Perl Weekly Challenge - 098" }, - "plotOptions" : { - "series" : { - "borderWidth" : 0, - "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - } - } + "subtitle" : { + "text" : "[Champions: 39] Last updated at 2021-02-09 22:16:58 GMT" }, "drilldown" : { "series" : [ { "name" : "Aaron Smith", - "id" : "Aaron Smith", "data" : [ [ "Raku", @@ -35,21 +233,20 @@ "Blog", 1 ] - ] + ], + "id" : "Aaron Smith" }, { - "name" : "Abigail", + "id" : "Abigail", "data" : [ [ "Perl", 2 ] ], - "id" : "Abigail" + "name" : "Abigail" }, { - "name" : "Adam Russell", - "id" : "Adam Russell", "data" : [ [ "Perl", @@ -59,9 +256,22 @@ "Blog", 2 ] - ] + ], + "id" : "Adam Russell", + "name" : "Adam Russell" }, { + "id" : "Alexander Pankoff", + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Alexander Pankoff" + }, + { + "name" : "Andinus", "data" : [ [ "Raku", @@ -72,11 +282,10 @@ 1 ] ], - "id" : "Andinus", - "name" : "Andinus" + "id" : "Andinus" }, { - "id" : "Arne Sommer", + "name" : "Arne Sommer", "data" : [ [ "Perl", @@ -91,10 +300,9 @@ 1 ] ], - "name" : "Arne Sommer" + "id" : "Arne Sommer" }, { - "id" : "Athanasius", "data" : [ [ "Perl", @@ -105,17 +313,28 @@ 2 ] ], + "id" : "Athanasius", "name" : "Athanasius" }, { + "data" : [ + [ + "Perl", + 1 + ] + ], + "id" : "Bob Lied", + "name" : "Bob Lied" + }, + { "name" : "Cheok-Yin Fung", - "id" : "Cheok-Yin Fung", "data" : [ [ "Perl", 1 ] - ] + ], + "id" : "Cheok-Yin Fung" }, { "id" : "Colin Crain", @@ -136,24 +355,24 @@ "name" : "Colin Crain" }, { - "name" : "Cristina Heredia", + "id" : "Cristina Heredia", "data" : [ [ "Perl", 2 ] ], - "id" : "Cristina Heredia" + "name" : "Cristina Heredia" }, { + "name" : "Dave Cross", "data" : [ [ "Perl", 2 ] ], - "id" : "Dave Cross", - "name" : "Dave Cross" + "id" : "Dave Cross" }, { "data" : [ @@ -170,26 +389,27 @@ "name" : "Dave Jacoby" }, { - "name" : "Duncan C. White", "data" : [ [ "Perl", 2 ] ], - "id" : "Duncan C. White" + "id" : "Duncan C. White", + "name" : "Duncan C. White" }, { - "id" : "E. Choroba", "data" : [ [ "Perl", 2 ] ], + "id" : "E. Choroba", "name" : "E. Choroba" }, { + "id" : "Flavio Poletti", "data" : [ [ "Perl", @@ -200,61 +420,78 @@ 2 ] ], - "id" : "Flavio Poletti", "name" : "Flavio Poletti" }, { "name" : "Gustavo Chaves", + "id" : "Gustavo Chaves", "data" : [ [ "Perl", 2 ] - ], - "id" : "Gustavo Chaves" + ] }, { + "name" : "Jaldhar H. Vyas", "data" : [ [ "Perl", 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 ] ], + "id" : "Jaldhar H. Vyas" + }, + { + "name" : "James Smith", "id" : "James Smith", - "name" : "James Smith" + "data" : [ + [ + "Perl", + 2 + ] + ] }, { - "name" : "Jan Krnavek", + "id" : "Jan Krnavek", "data" : [ [ "Raku", 2 ] ], - "id" : "Jan Krnavek" + "name" : "Jan Krnavek" }, { + "name" : "Joan Mimosinnet", "id" : "Joan Mimosinnet", "data" : [ [ "Raku", 2 ] - ], - "name" : "Joan Mimosinnet" + ] }, { - "name" : "Jorg Sommrey", - "id" : "Jorg Sommrey", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey" }, { - "name" : "Laurent Rosenfeld", + "id" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -269,16 +506,16 @@ 1 ] ], - "id" : "Laurent Rosenfeld" + "name" : "Laurent Rosenfeld" }, { - "id" : "Lubos Kolouch", "data" : [ [ "Perl", 2 ] ], + "id" : "Lubos Kolouch", "name" : "Lubos Kolouch" }, { @@ -306,24 +543,24 @@ "id" : "Mark Anderson" }, { - "name" : "Markus Holzer", - "id" : "Markus Holzer", "data" : [ [ "Raku", 2 ] - ] + ], + "id" : "Markus Holzer", + "name" : "Markus Holzer" }, { + "name" : "Niels van Dijke", "id" : "Niels van Dijke", "data" : [ [ "Perl", 2 ] - ], - "name" : "Niels van Dijke" + ] }, { "name" : "Nuno Vieira", @@ -336,14 +573,14 @@ "id" : "Nuno Vieira" }, { - "id" : "Paulo Custodio", + "name" : "Paulo Custodio", "data" : [ [ "Perl", 2 ] ], - "name" : "Paulo Custodio" + "id" : "Paulo Custodio" }, { "id" : "Pete Houston", @@ -356,14 +593,14 @@ "name" : "Pete Houston" }, { + "name" : "Philip Hood", + "id" : "Philip Hood", "data" : [ [ "Raku", 2 ] - ], - "id" : "Philip Hood", - "name" : "Philip Hood" + ] }, { "name" : "Roger Bell_West", @@ -384,7 +621,6 @@ "id" : "Roger Bell_West" }, { - "id" : "Simon Green", "data" : [ [ "Perl", @@ -395,19 +631,21 @@ 1 ] ], + "id" : "Simon Green", "name" : "Simon Green" }, { "name" : "Simon Proctor", + "id" : "Simon Proctor", "data" : [ [ "Raku", 2 ] - ], - "id" : "Simon Proctor" + ] }, { + "name" : "Stuart Little", "id" : "Stuart Little", "data" : [ [ @@ -418,12 +656,10 @@ "Raku", 2 ] - ], - "name" : "Stuart Little" + ] }, { "name" : "Ulrich Rieke", - "id" : "Ulrich Rieke", "data" : [ [ "Perl", @@ -433,10 +669,11 @@ "Raku", 1 ] - ] + ], + "id" : "Ulrich Rieke" }, { - "id" : "W. Luis Mochan", + "name" : "W. Luis Mochan", "data" : [ [ "Perl", @@ -447,216 +684,32 @@ 1 ] ], - "name" : "W. Luis Mochan" + "id" : "W. Luis Mochan" }, { - "name" : "Wanderdoc", "data" : [ [ "Perl", 2 ] ], - "id" : "Wanderdoc" + "id" : "Wanderdoc", + "name" : "Wanderdoc" } ] }, - "series" : [ - { - "name" : "Perl Weekly Challenge - 098", - "data" : [ - { - "y" : 3, - "drilldown" : "Aaron Smith", - "name" : "Aaron Smith" - }, - { - "name" : "Abigail", - "drilldown" : "Abigail", - "y" : 2 - }, - { - "y" : 4, - "name" : "Adam Russell", - "drilldown" : "Adam Russell" - }, - { - "name" : "Andinus", - "drilldown" : "Andinus", - "y" : 2 - }, |
