From 908aa39a6d95d19cbe8f59dc56918676ccca5976 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 11 Dec 2023 16:08:54 -0500 Subject: solved 247 --- challenge-247/dave-jacoby/blog.txt | 1 + challenge-247/dave-jacoby/perl/ch-1.pl | 62 ++++++++++++++++++++++++++++++++++ challenge-247/dave-jacoby/perl/ch-2.pl | 32 ++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 challenge-247/dave-jacoby/blog.txt create mode 100644 challenge-247/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-247/dave-jacoby/perl/ch-2.pl diff --git a/challenge-247/dave-jacoby/blog.txt b/challenge-247/dave-jacoby/blog.txt new file mode 100644 index 0000000000..6bd07983bc --- /dev/null +++ b/challenge-247/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/12/11/partidges-and-pair-trees-weekly-challenge-247.html diff --git a/challenge-247/dave-jacoby/perl/ch-1.pl b/challenge-247/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..d96619aa19 --- /dev/null +++ b/challenge-247/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ + 'Mr. Wall', + 'Mrs. Wall', + 'Mr. Anwar', + 'Mrs. Anwar', + 'Mr. Conway', + 'Mr. Cross', + ], + + [ 'Mr. Wall', 'Mrs. Wall', 'Mr. Anwar', ], +); + +for my $example (@examples) { + my %output = secret_santa( $example->@* ); + my $input = join ",\n\t", + map { qq{"$_"} } # quote surname + map { $_->[0] } # remove surname element + sort { $a->[1] cmp $b->[1] } # sort on surname + map { [ $_, ( reverse split /\s/, $_ )[0] ] } # start schartzian transform on surname + sort { $a cmp $b } $example->@*; # sort alphabetically for consistency + my $output = join "\n\t", + map { qq{$_ -> $output{$_}} } # combine santa and giftee + map { $_->[0] } # remove surname element + sort { $a->[1] cmp $b->[1] } # sort on surname + map { [ $_, ( reverse split /\s/, $_ )[0] ] } # start schartzian transform on surname + sort { $a cmp $b } keys %output; # sort alphabetically for consistency + + say <<~"END"; + Input: \$input = ( + $input + ); + Output: + $output + END + +} + +# 1) everybody gets matched +# 2) nobody gets matched to themself +sub secret_santa (@input) { + my %done; + + for my $name (@input) { + my %chosen = reverse %done; + my @others = + sort { rand 10 <=> rand 10 } + grep { $_ ne $name } @input; + for my $giftee (@others) { + next if $chosen{$giftee}; + $done{$name} = $giftee; + } + } + return %done; +} diff --git a/challenge-247/dave-jacoby/perl/ch-2.pl b/challenge-247/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..cdc7dabc15 --- /dev/null +++ b/challenge-247/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + 'abcdbca', + 'cdeabeabfcdfabgcd', +); + +for my $e (@examples) { + my $output = most_frequent_letter_pair($e); + + say <<~"END"; + Input: \$input = '$e' + Output: '$output' + END +} + +sub most_frequent_letter_pair ($string) { + my %data; + for my $i ( 0 .. -2 + length $string ) { + my $sub = substr $string, $i, 2; + $data{$sub}++; + } + # ($scalar) = @list will assign the first element in the list to $scalar + my ($first) = sort { $data{$b} <=> $data{$a} } # second sort on value + sort keys %data; # first sort on lexographic value + return $first; +} -- cgit From 83d7427ab4acd5848bba098660eff4973d826132 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Sun, 17 Dec 2023 21:04:54 -0600 Subject: PWC 248 Task 2 complete --- challenge-248/bob-lied/perl/ch-2.pl | 75 +++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 challenge-248/bob-lied/perl/ch-2.pl diff --git a/challenge-248/bob-lied/perl/ch-2.pl b/challenge-248/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..dc98c4d1aa --- /dev/null +++ b/challenge-248/bob-lied/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# +# ch-2.pl Perl Weekly Challenge 248 Task 2 Submatrix Sum +#============================================================================= +# You are given a NxM matrix A of integers. +# Write a script to construct a (N-1)x(M-1) matrix B having elements that +# are the sum over the 2x2 submatrices of A, +# b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1] +# Example 1 Input: $a = [ [1, 2, 3, 4], +# [5, 6, 7, 8], +# [9, 10, 11, 12] ] +# Output: $b = [ [14, 18, 22], +# [30, 34, 38] ] +# Example 2 Input: $a = [ [1, 0, 0, 0], +# [0, 1, 0, 0], +# [0, 0, 1, 0], +# [0, 0, 0, 1] ] +# Output: $b = [ [2, 1, 0], +# [1, 2, 1], +# [0, 1, 2] ] +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub sms($m) +{ + my $height = $m->$#*; + my $width = $m->[0]->$#*; + + my @output; + push @output, [ (0) x $width ] for 0 .. $height -1; + + for my $i ( 0 .. $height-1 ) + { + for my $k ( 0 .. $width-1 ) + { + $output[$i][$k] = $m->[$i ][$k] + $m->[$i ][$k+1] + + $m->[$i+1][$k] + $m->[$i+1][$k+1]; + } + } + return \@output; +} + +sub runTest +{ + use Test2::V0; + + is( sms( [ [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12] ]), + [ [14, 18, 22], [30, 34, 38] ] , "Example 1"); + + is( sms( [ [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1] ] ), + [ [2, 1, 0], + [1, 2, 1], + [0, 1, 2] ], "Example 2"); + + done_testing; +} -- cgit From 4dba02dedbacd283e92df9cb6f5c52ea844a25a1 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Sun, 17 Dec 2023 21:34:08 -0600 Subject: Update README --- challenge-248/bob-lied/README | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-248/bob-lied/README b/challenge-248/bob-lied/README index ddf6e99243..882a98a265 100644 --- a/challenge-248/bob-lied/README +++ b/challenge-248/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 247 by Bob Lied +Solutions to weekly challenge 248 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-247/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-247/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-248/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-248/bob-lied -- cgit From 5e14bb89fcebf48966afd275e2bff8eb523fa6af Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Sun, 17 Dec 2023 21:34:19 -0600 Subject: PWC 248 Task 1 complete --- challenge-248/bob-lied/perl/ch-1.pl | 72 +++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 challenge-248/bob-lied/perl/ch-1.pl diff --git a/challenge-248/bob-lied/perl/ch-1.pl b/challenge-248/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..4214acf1f2 --- /dev/null +++ b/challenge-248/bob-lied/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 248 Task 1 Shortest Distance +#============================================================================= +# You are given a string and a character in the given string. +# Write a script to return an array of integers of size same as length of +# the given string such that: +# distance[i] is the distance from index i to the closest occurence of +# the given character in the given string. +# The distance between two indices i and j is abs(i - j). +# Example 1 Input: $str = "loveleetcode", $char = "e" +# Output: (3,2,1,0,1,0,0,1,2,2,1,0) +# The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed). +# The closest occurrence of 'e' for index 0 is at index 3, +# so the distance is abs(0 - 3) = 3. +# The closest occurrence of 'e' for index 1 is at index 3, +# so the distance is abs(1 - 3) = 2. +# For index 4, there is a tie between the 'e' at index 3 and 'e' at index 5, +# but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1. +# The closest occurrence of 'e' for index 8 is at index 6, +# so the distance is abs(8 - 6) = 2. +# Example 2 Input: $str = "aaab", $char = "b" +# Output: (3,2,1,0) +#============================================================================= + +use v5.38; + +use builtin qw/true false/; no warnings "experimental::builtin"; + +use List::Util qw/min/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say "(", join(",", shortest(@ARGV)->@*), ")"; + +sub shortest($str, $char) +{ + my @dist; + my @s = split //, $str; # str as vector of characters + + # List of indexes where char appears + my @cloc = grep { $s[$_] eq $char } 0 .. $#s; + + for my $i ( 0 .. $#s ) # For each letter in str + { + # List of location differences + # vvvvvvvvvvvvvvvvvvvvvvvvvvvv + push @dist, min map { abs($_ - $i) } @cloc; + } + return \@dist; +} + +sub runTest +{ + use Test2::V0; + + is( shortest("loveleetcode", 'e'), [3,2,1,0,1,0,0,1,2,2,1,0], "Example 1"); + is( shortest("aaab", 'b'), [3,2,1,0], "Example 2"); + + is( shortest("ab", 'x'), [undef, undef], "no x in str"); + is( shortest("", 'x'), [], "empty string"); + + done_testing; +} -- cgit From 75e2e0e2044ec402e661607746312bda43d38d7b Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Sun, 17 Dec 2023 21:46:09 -0600 Subject: PWC 248 Task 1 alternate solution using index --- challenge-248/bob-lied/perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/challenge-248/bob-lied/perl/ch-1.pl b/challenge-248/bob-lied/perl/ch-1.pl index 4214acf1f2..2f18387091 100644 --- a/challenge-248/bob-lied/perl/ch-1.pl +++ b/challenge-248/bob-lied/perl/ch-1.pl @@ -58,6 +58,28 @@ sub shortest($str, $char) return \@dist; } +sub sd2($str, $char) +{ + my @dist; + for my $i ( 0 .. length($str)-1 ) + { + my $ahead = index($str, "$char", $i); + my $behind = rindex($str, "$char", $i); + + if ( $ahead < 0 && $behind < 0 ) + { + push @dist, undef; + } + else + { + $behind = $ahead if $behind == -1; + $ahead = $behind if $ahead == -1; + push @dist, min abs($i - $behind), abs($ahead - $i); + } + } + return \@dist; +} + sub runTest { use Test2::V0; @@ -68,5 +90,11 @@ sub runTest is( shortest("ab", 'x'), [undef, undef], "no x in str"); is( shortest("", 'x'), [], "empty string"); + is( sd2("loveleetcode", 'e'), [3,2,1,0,1,0,0,1,2,2,1,0], "Example 1"); + is( sd2("aaab", 'b'), [3,2,1,0], "Example 2"); + + is( sd2("ab", 'x'), [undef, undef], "no x in str"); + is( sd2("", 'x'), [], "empty string"); + done_testing; } -- cgit From 268bf42627a7fe696e04881b794a0e17455bebed Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Sun, 17 Dec 2023 22:47:02 -0600 Subject: PWC 248 task 1 third alternative --- challenge-248/bob-lied/perl/ch-1.pl | 52 +++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/challenge-248/bob-lied/perl/ch-1.pl b/challenge-248/bob-lied/perl/ch-1.pl index 2f18387091..e6328dfcff 100644 --- a/challenge-248/bob-lied/perl/ch-1.pl +++ b/challenge-248/bob-lied/perl/ch-1.pl @@ -28,7 +28,7 @@ use v5.38; -use builtin qw/true false/; no warnings "experimental::builtin"; +use builtin qw/true false ceil floor/; no warnings "experimental::builtin"; use List::Util qw/min/; @@ -49,6 +49,8 @@ sub shortest($str, $char) # List of indexes where char appears my @cloc = grep { $s[$_] eq $char } 0 .. $#s; + # Potentially a lot of useless array operations, math + # and comparisons if char appears a lot. for my $i ( 0 .. $#s ) # For each letter in str { # List of location differences @@ -58,6 +60,11 @@ sub shortest($str, $char) return \@dist; } +# Only two location differences really matter: the next one +# ahead or the last one behind. Alternate implementation +# looks only for those two. Potentially a lot of string +# scanning if there are very few occurences of char in a +# long string. sub sd2($str, $char) { my @dist; @@ -80,6 +87,35 @@ sub sd2($str, $char) return \@dist; } +# We don't really need to calculate the differences for each +# letter. The distance counts down until we see the first occurence, +# then up until half way to the next occurrence, then down again. +# Given the locations of the character, we can generate the sequences. +sub sd3($str, $char) +{ + return [ (undef) x length($str) ] if index($str, $char) < 0; + my @s = split //, $str; # str as vector of characters + + # List of indexes where char appears + my @cloc = grep { $s[$_] eq $char } 0 .. $#s; + + my @dist; + my $loc = shift @cloc; + push @dist, reverse 0 .. $loc; + while ( defined(my $next = shift @cloc) ) + { + my $diff = $next - $loc -1; + push @dist, 1 .. ceil($diff/2); + push @dist, reverse( 0 .. floor($diff/2)); + $loc = $next; + } + if ( $loc < $#s ) + { + push @dist, 1 .. ($#s - $loc); + } + return \@dist; +} + sub runTest { use Test2::V0; @@ -90,11 +126,17 @@ sub runTest is( shortest("ab", 'x'), [undef, undef], "no x in str"); is( shortest("", 'x'), [], "empty string"); - is( sd2("loveleetcode", 'e'), [3,2,1,0,1,0,0,1,2,2,1,0], "Example 1"); - is( sd2("aaab", 'b'), [3,2,1,0], "Example 2"); + is( sd2("loveleetcode", 'e'), [3,2,1,0,1,0,0,1,2,2,1,0], "sd2 Example 1"); + is( sd2("aaab", 'b'), [3,2,1,0], "sd2 Example 2"); + + is( sd2("ab", 'x'), [undef, undef], "sd2 no x in str"); + is( sd2("", 'x'), [], "sd2 empty string"); + + is( sd3("loveleetcode", 'e'), [3,2,1,0,1,0,0,1,2,2,1,0], "sd3 Example 1"); + is( sd3("aaab", 'b'), [3,2,1,0], "sd3 Example 2"); - is( sd2("ab", 'x'), [undef, undef], "no x in str"); - is( sd2("", 'x'), [], "empty string"); + is( sd3("ab", 'x'), [undef, undef], "sd3 no x in str"); + is( sd3("", 'x'), [], "sd3 empty string"); done_testing; } -- cgit From 366baf745428924a7ebea30d0a4be57135dabfce Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Sun, 17 Dec 2023 22:54:14 -0600 Subject: AOC 248 ch-1.pl combine push lines --- challenge-248/bob-lied/perl/ch-1.pl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/challenge-248/bob-lied/perl/ch-1.pl b/challenge-248/bob-lied/perl/ch-1.pl index e6328dfcff..e202a54191 100644 --- a/challenge-248/bob-lied/perl/ch-1.pl +++ b/challenge-248/bob-lied/perl/ch-1.pl @@ -105,8 +105,7 @@ sub sd3($str, $char) while ( defined(my $next = shift @cloc) ) { my $diff = $next - $loc -1; - push @dist, 1 .. ceil($diff/2); - push @dist, reverse( 0 .. floor($diff/2)); + push @dist, (1 .. ceil($diff/2)), reverse( 0 .. floor($diff/2)); $loc = $next; } if ( $loc < $#s ) -- cgit From 94ecb68a2c33fe827b6f4999eea7825d0cbb2ec1 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Sun, 17 Dec 2023 23:57:17 -0500 Subject: Week 248 --- challenge-248/zapwai/perl/ch-1.pl | 19 +++++++++++++++++++ challenge-248/zapwai/perl/ch-2.pl | 28 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 challenge-248/zapwai/perl/ch-1.pl create mode 100644 challenge-248/zapwai/perl/ch-2.pl diff --git a/challenge-248/zapwai/perl/ch-1.pl b/challenge-248/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..00e161cc78 --- /dev/null +++ b/challenge-248/zapwai/perl/ch-1.pl @@ -0,0 +1,19 @@ +use v5.30; +my @ints = (3,2,1,4); +say "Input: \@ints = (".join(", ", @ints).")"; +my ($min, $max) = (1000000, 0); +for my $current (@ints) { + $max = $current if ($max < $current); + $min = $current if ($min > $current); +} +my @bad; +for my $i (0 .. $#ints) { + if (($ints[$i] == $max) or ($ints[$i] == $min) ) { + push @bad, $i; + } +} +splice @ints, $_, 1 foreach (reverse @bad); +print "Output: "; + +my $out = (!@ints) ? "-1" : join(", ",@ints); +say $out; diff --git a/challenge-248/zapwai/perl/ch-2.pl b/challenge-248/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..9272cf60ba --- /dev/null +++ b/challenge-248/zapwai/perl/ch-2.pl @@ -0,0 +1,28 @@ +use v5.30; +my $a = [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12] +]; +my $n = @$a - 1; +my $m = @{$$a[0]} - 1; +say "Input: \$a = ["; +for (0 .. $n) { + say "\t[" . join(",", @{$$a[$_]}) ."]"; +} +say "\t]"; +my @b; +for my $i (0 .. $n - 1) { + for my $j (0 .. $m - 1) { + $$b[$i][$j] = subsum($i,$j); + } +} +say "Output: \$b = ["; +for (0 .. $n - 1) { + say "\t[" . join(",", @{$$b[$_]}) ."]"; +} +say "\t]"; +sub subsum { + my ($i, $j) = @_; + return $$a[$i][$j] + $$a[$i+1][$j] + $$a[$i][$j+1] + $$a[$i+1][$j+1]; +} -- cgit From dc1854fdace1827f16f2d3dde1e403de2003ef38 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Sun, 17 Dec 2023 23:01:05 -0600 Subject: Solve PWC248 --- challenge-248/wlmb/blog.txt | 1 + challenge-248/wlmb/perl/ch-1.pl | 15 +++++++++++++++ challenge-248/wlmb/perl/ch-2.pl | 16 ++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 challenge-248/wlmb/blog.txt create mode 100755 challenge-248/wlmb/perl/ch-1.pl create mode 100755 challenge-248/wlmb/perl/ch-2.pl diff --git a/challenge-248/wlmb/blog.txt b/challenge-248/wlmb/blog.txt new file mode 100644 index 0000000000..2638288046 --- /dev/null +++ b/challenge-248/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/12/17/PWC248/ diff --git a/challenge-248/wlmb/perl/ch-1.pl b/challenge-248/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..14c9ab3c0e --- /dev/null +++ b/challenge-248/wlmb/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +# Perl weekly challenge 248 +# Task 1: Shortest Distance +# +# See https://wlmb.github.io/2023/12/17/PWC248/#task-1-shortest-distance +use v5.36; +use PDL; +die <<~"FIN" unless @ARGV==2; + Usage: $0 string letter + to obtain the shortest distances. + FIN +my @letters=split "", $ARGV[0]; +my $i=pdl(grep {$letters[$_] eq $ARGV[1]} 0..@letters-1); +my $j=sequence(0+@letters); +say "@ARGV; $ARGV[1] -> ", ($i->dummy(1)-$j->dummy(0))->abs->minover; diff --git a/challenge-248/wlmb/perl/ch-2.pl b/challenge-248/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..7d9022d2fd --- /dev/null +++ b/challenge-248/wlmb/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +# Perl weekly challenge 248 +# Task 2: Submatrix Sum +# +# See https://wlmb.github.io/2023/12/17/PWC248/#task-2-submatrix-sum +use v5.36; +use PDL; +use PDL::NiceSlice; +die <<~"FIN" unless @ARGV==1; + Usage: $0 M + to sum 2x2 the overlapped submatrices of matrix M + FIN +my $m=pdl(shift); +my $n=pdl($m(0:-2,0:-2),$m(1:-1,0:-2),$m(0:-2,1:-1),$m(1:-1,1:-1)) + ->mv(-1,0)->sumover; +say "$m->$n" -- cgit From ac5e4011d54b977a0251a10a5971b348958b798b Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 18 Dec 2023 09:09:55 +0000 Subject: Challenge 248 Solutions (Raku) --- challenge-248/mark-anderson/raku/ch-1.raku | 46 +++++++++++++++++++ challenge-248/mark-anderson/raku/ch-2.raku | 72 ++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 challenge-248/mark-anderson/raku/ch-1.raku create mode 100644 challenge-248/mark-anderson/raku/ch-2.raku diff --git a/challenge-248/mark-anderson/raku/ch-1.raku b/challenge-248/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..9f9cfc21e3 --- /dev/null +++ b/challenge-248/mark-anderson/raku/ch-1.raku @@ -0,0 +1,46 @@ +#!/usr/bin/env raku +use Test; + +is-deeply shortest-distance("loveleetcode", "e"), (3,2,1,0,1,0,0,1,2,2,1,0); +is-deeply shortest-distance("aaab", "b"), (3,2,1,0); +is-deeply shortest-distance("eabcde", "e"), (0,1,2,2,1,0); +is-deeply shortest-distance("eabcdf", "e"), (0,1,2,3,4,5); +is-deeply shortest-distance("abecd", "e"), (2,1,0,1,2); +is-deeply shortest-distance("abcefg", "e"), (3,2,1,0,1,2); +is-deeply shortest-distance("eeeabecefeeg", "e"), (0,0,0,1,1,0,1,0,1,0,0,1); + +sub shortest-distance($str, $char) +{ + my @a = $str.split(/<$char>+/, :v); + + my $head = @a.shift; + $_ = .chars ?? (.chars...1) !! Empty given $head; + + my $tail = @a.pop; + $_ = .chars ?? (1....chars) !! Empty given $tail; + + @a.push: ~Empty; + + my $middle = gather for @a -> $a, $b + { + take 0 xx $a.chars; + take distance($b) + } + + flat ($head, $middle, $tail)>>.List +} + +sub distance($s) +{ + return Empty unless $s; + my $mid = ($s.chars / 2).ceiling; + my @r = (1...^$mid), ($mid, $mid), ($mid^...1); + @r[1] .= squish unless $s.chars %% 2; + @r.List +} + +# sub shortest-distance-check($str, $char) +# { +# my @zeros = $str.indices($char); +# (^$str.chars).map(-> $k { min @zeros.map({ abs($_ - $k) }) }) +# } diff --git a/challenge-248/mark-anderson/raku/ch-2.raku b/challenge-248/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..60784f7d98 --- /dev/null +++ b/challenge-248/mark-anderson/raku/ch-2.raku @@ -0,0 +1,72 @@ +#!/usr/bin/env raku +use Test; + +is-deeply submatrix-sum([ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12] + ]), + + [ + [14, 18, 22], + [30, 34, 38], + ]; + +is-deeply submatrix-sum([ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1] + ]), + + [ + [2, 1, 0], + [1, 2, 1], + [0, 1, 2] + ]; + +is-deeply submatrix-sum([ + [ 22, 4, 20, 24 ], + [ 21, 16, 23, 18 ], + [ 3, 12, 19, 5 ], + [ 15, 7, 10, 2 ], + [ 17, 11, 6, 13 ], + [ 9, 8, 14, 1 ] + ]), + + [ + [ 63, 63, 85 ], + [ 52, 70, 65 ], + [ 37, 48, 36 ], + [ 50, 34, 31 ], + [ 45, 39, 34 ] + ]; + +is-deeply submatrix-sum([ + [ 26, 50, 10, 54, 42, 31, 45, 58 ], + [ 29, 64, 23, 35, 15, 41, 27, 17 ], + [ 55, 37, 1, 30, 52, 53, 21, 46 ], + [ 3, 2, 6, 36, 11, 13, 19, 22 ], + [ 61, 47, 32, 39, 49, 62, 9, 12 ], + [ 20, 60, 44, 7, 59, 34, 14, 16 ], + [ 8, 24, 38, 43, 56, 48, 57, 25 ], + [ 5, 40, 33, 28, 63, 18, 4, 51 ] + ]), + + [ + [ 169, 147, 122, 146, 129, 144, 147 ], + [ 185, 125, 89, 132, 161, 142, 111 ], + [ 97, 46, 73, 129, 129, 106, 108 ], + [ 113, 87, 113, 135, 135, 103, 62 ], + [ 188, 183, 122, 154, 204, 119, 51 ], + [ 112, 166, 132, 165, 197, 153, 112 ], + [ 77, 135, 142, 190, 185, 127, 137 ] + ]; + +sub submatrix-sum(@a) +{ + @a.map({ .rotor(2 => -1)>>.Array }) + .rotor(2 => -1) + .map({ (zip $_).map({ .flat.sum }).Array }) + .Array +} -- cgit From e5c6087814a9ecd97539337c40f29f68c718aa40 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 18 Dec 2023 09:34:01 +0100 Subject: PWC 248 Task 1 Raku done Task 2 Raku done Task 1 PL/Perl done Task 2 PL/Perl done Task 1 PL/PgSQL done Task 2 PL/PgSQL done Task 1 Python done Task 2 Python done --- challenge-248/luca-ferrari/blog-1.txt | 1 + challenge-248/luca-ferrari/blog-2.txt | 1 + challenge-248/luca-ferrari/blog-3.txt | 1 + challenge-248/luca-ferrari/blog-4.txt | 1 + challenge-248/luca-ferrari/blog-5.txt | 1 + challenge-248/luca-ferrari/blog-6.txt | 1 + challenge-248/luca-ferrari/blog-7.txt | 1 + challenge-248/luca-ferrari/blog-8.txt | 1 + challenge-248/luca-ferrari/postgresql/ch-1.plperl | 52 +++++++++++++++++++++++ challenge-248/luca-ferrari/postgresql/ch-1.sql | 31 ++++++++++++++ challenge-248/luca-ferrari/postgresql/ch-2.plperl | 28 ++++++++++++ challenge-248/luca-ferrari/postgresql/ch-2.sql | 32 ++++++++++++++ challenge-248/luca-ferrari/python/ch-1.py | 37 ++++++++++++++++ challenge-248/luca-ferrari/python/ch-2.py | 42 ++++++++++++++++++ challenge-248/luca-ferrari/raku/ch-1.p6 | 24 +++++++++++ challenge-248/luca-ferrari/raku/ch-2.p6 | 27 ++++++++++++ 16 files changed, 281 insertions(+) create mode 100644 challenge-248/luca-ferrari/blog-1.txt create mode 100644 challenge-248/luca-ferrari/blog-2.txt create mode 100644 challenge-248/luca-ferrari/blog-3.txt create mode 100644 challenge-248/luca-ferrari/blog-4.txt create mode 100644 challenge-248/luca-ferrari/blog-5.txt create mode 100644 challenge-248/luca-ferrari/blog-6.txt create mode 100644 challenge-248/luca-ferrari/blog-7.txt create mode 100644 challenge-248/luca-ferrari/blog-8.txt create mode 100644 challenge-248/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-248/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-248/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-248/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-248/luca-ferrari/python/ch-1.py create mode 100644 challenge-248/luca-ferrari/python/ch-2.py create mode 100644 challenge-248/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-248/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-248/luca-ferrari/blog-1.txt b/challenge-248/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..5b808ae612 --- /dev/null +++ b/challenge-248/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task1 diff --git a/challenge-248/luca-ferrari/blog-2.txt b/challenge-248/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..9c7687d808 --- /dev/null +++ b/challenge-248/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task2 diff --git a/challenge-248/luca-ferrari/blog-3.txt b/challenge-248/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..d253c3c55e --- /dev/null +++ b/challenge-248/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task1plperl diff --git a/challenge-248/luca-ferrari/blog-4.txt b/challenge-248/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..a061387068 --- /dev/null +++ b/challenge-248/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task2plperl diff --git a/challenge-248/luca-ferrari/blog-5.txt b/challenge-248/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..b25bbbb900 --- /dev/null +++ b/challenge-248/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task1plpgsql diff --git a/challenge-248/luca-ferrari/blog-6.txt b/challenge-248/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..b598a001f1 --- /dev/null +++ b/challenge-248/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task2plpgsql diff --git a/challenge-248/luca-ferrari/blog-7.txt b/challenge-248/luca-ferrari/blog-7.txt new file mode 100644 index 0000000000..1aa9fe6d54 --- /dev/null +++ b/challenge-248/luca-ferrari/blog-7.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task1python diff --git a/challenge-248/luca-ferrari/blog-8.txt b/challenge-248/luca-ferrari/blog-8.txt new file mode 100644 index 0000000000..726a79305a --- /dev/null +++ b/challenge-248/luca-ferrari/blog-8.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/12/18/PerlWeeklyChallenge248.html#task2python diff --git a/challenge-248/luca-ferrari/postgresql/ch-1.plperl b/challenge-248/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..9a818cbc44 --- /dev/null +++ b/challenge-248/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,52 @@ +-- +-- Perl Weekly Challenge 248 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc248; + +CREATE OR REPLACE FUNCTION +pwc248.task1_plperl( text, text ) +RETURNS TABLE( c char, distance int ) +AS $CODE$ + my ( $string, $char ) = @_; + + die "Use a single char!" if ( length( $char ) > 1 ); + die "Not matching at all!" if ( $string !~ / $char /x ); + + + my $min = sub { + my $current_min = $_[ 0 ]; + shift; + for ( @_ ) { + $current_min = $_ if ( $_ < $current_min ); + } + + return $current_min; + }; + + my @distances; + my @matching; + + my @letters = split //, $string; + for ( 0 .. @letters - 1 ) { + next if $letters[ $_ ] ne $char; + push @matching, $_; + } + + + for my $me ( 0 .. @letters - 1 ) { + + $distances[ $me ] = 0 if ( $letters[ $me ] eq $char ); + $distances[ $me ] = $min->( map { abs( $me - $_ ) } @matching ) if ( $letters[ $me ] ne $char ); + + return_next( { c => $letters[ $me ], + distance => $distances[ $me ] } ); + + } + + return undef; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-248/luca-ferrari/postgresql/ch-1.sql b/challenge-248/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..62d2638eb4 --- /dev/null +++ b/challenge-248/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,31 @@ +-- +-- Perl Weekly Challenge 248 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc248; + +CREATE OR REPLACE FUNCTION +pwc248.task1_plpgsql( s text, c char ) +RETURNS TABLE ( cc char, distance int ) +AS $CODE$ +DECLARE +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS distances( cc char, ind int ); + TRUNCATE distances; + + INSERT INTO distances + SELECT v, row_number() over () + FROM regexp_split_to_table( s, '' ) v; + + + RETURN QUERY + SELECT d.cc, ( SELECT min( abs( d2.ind - d.ind ) ) + FROM distances d2 + WHERE d2.cc = c ) + FROM distances d; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-248/luca-ferrari/postgresql/ch-2.plperl b/challenge-248/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..324f20f373 --- /dev/null +++ b/challenge-248/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,28 @@ +-- +-- Perl Weekly Challenge 248 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc248; + +CREATE OR REPLACE FUNCTION +pwc248.task2_plperl( int[] ) +RETURNS int[] +AS $CODE$ + my ( $matrix ) = @_; + + my $result = []; + + for my $row ( 0 .. $matrix->@* - 1 ) { + $result->[ $row ] = []; + + for my $col ( 0 .. $matrix->@* - 1 ) { + $result->[ $row ][ $col ] = $matrix->[ $row ][ $col ] + $matrix->[ $row ][ $col + 1 ] + $matrix->[ $row + 1 ][ $col ] + $matrix->[ $row + 1 ][ $col + 1 ]; + } + } + + return $result; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-248/luca-ferrari/postgresql/ch-2.sql b/challenge-248/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..c8d84fe66b --- /dev/null +++ b/challenge-248/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,32 @@ +-- +-- Perl Weekly Challenge 248 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc248; + +CREATE OR REPLACE FUNCTION +pwc248.task2_plpgsql( matrix int[] ) +RETURNS SETOF int[] +AS $CODE$ +DECLARE + current_row int[]; +BEGIN + + FOR r IN 1 .. array_length( matrix, 1 ) - 1 LOOP + + current_row = array[ array_length( matrix, 1 ) ]; + + FOR c IN 1 .. array_length( matrix, 2 ) - 1 LOOP + current_row[ c ] = matrix[ r ][ c ] + matrix[ r ][ c + 1 ] + matrix[ r + 1 ][ c ] + matrix[ r + 1 ][ c + 1 ]; + END LOOP; + + RETURN NEXT current_row; + END LOOP; + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-248/luca-ferrari/python/ch-1.py b/challenge-248/luca-ferrari/python/ch-1.py new file mode 100644 index 0000000000..e43a827ec1 --- /dev/null +++ b/challenge-248/luca-ferrari/python/ch-1.py @@ -0,0 +1,37 @@ +#!python + +# +# Perl Weekly Challenge 248 +# Task 1 +# +# See +# + +import sys + +# task implementation +def main( argv ): + string = argv[ 0 ] + char = argv[ 1 ] + + matching = [] + for i in range( 0, len( string ) ): + c = string[ i ] + if c == char: + matching.append( i ) + + distances = [] + for i in range( 0, len( string ) ): + if string[ i ] == char: + distances.append( 0 ) + else: + distances.append( min( map( lambda x: abs( i - x ), matching ) ) ) + + print( ', '.join( map( str, distances ) ) ) + + +# invoke the main without the command itself +if __name__ == '__main__': + main( sys.argv[ 1: ] ) + + diff --git a/challenge-248/luca-ferrari/python/ch-2.py b/challenge-248/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..e056a1b792 --- /dev/null +++ b/challenge-248/luca-ferrari/python/ch-2.py @@ -0,0 +1,42 @@ +#!python + +# +# Perl Weekly Challenge 248 +# Task 2 +# +# See +# + +import sys + +# task implementation +def main( argv ): + matrix = argv + output = [] + + for r in range( 0, len( matrix ) - 1 ): + + output.append( [] ) + + for c in range( 0, len( matrix[ r ] ) - 1 ): + output[ r ].append( matrix[ r ][ c ] + matrix[ r ][ c + 1 ] + matrix[ r + 1 ][ c ] + matrix[ r + 1 ][ c + 1 ] ) + + print( "\n".join( map( str, output ) ) ) + + +# invoke the main without the command itself +if __name__ == '__main__': + matrix = [ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, 0], + [0, 0, 0, 1] + ] + + matrix = [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12] + ] + + main( matrix ) diff --git a/challenge-248/luca-ferrari/raku/ch-1.p6 b/challenge-248/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..893cd908a6 --- /dev/null +++ b/challenge-248/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,24 @@ +#!raku + +# +# Perl Weekly Challenge 248 +# Task 1 +# +# See +# + +sub MAIN( Str :$string, Str :$char where { $char.chars == 1 } ) { + + my @distances; + my @matching; + my @letters = $string.comb; + + @matching.push: $_ if ( $char eq @letters[ $_ ] ) for 0 ..^ @letters.elems; + + for 0 ..^ @letters.elems -> $me { + @distances[ $me ] = 0 and next if ( $char ~~ @letters[ $me ] ); + @distances[ $me ] = @matching.map( { abs( $_ - $me ) } ).min; + } + + @distances.join( ', ' ).say; +} diff --git a/challenge-248/luca-ferrari/raku/ch-2.p6 b/challenge-248/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..7e5aee79e7 --- /dev/null +++ b/challenge-248/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,27 @@ +#!raku + +# +# Perl Weekly Challenge 248 +# Task 2 +# +# See +# + +sub MAIN() { + my $a = [ + [1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12] + ]; + + + my $b = []; + for 0 ..^ $a.elems - 1 -> $row { + $b[ $row ].push: []; + for 0 ..^ $a[ $row ].elems - 1 -> $col { + $b[ $row ][ $col ] = $a[ $row ][ $col ] + $a[ $row ][ $col + 1 ] + $a[ $row + 1 ][ $col ] + $a[ $row + 1 ][ $col + 1 ]; + } + } + + $b.join( "\n" ).say; +} -- cgit From 5f62b3ed1b23c5455f09ad1a09f087daa3e55f3e Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 18 Dec 2023 09:39:42 +0000 Subject: Challenge 248 Solutions (Raku) --- challenge-248/mark-anderson/raku/ch-2.raku | 52 +++++++++++++++--------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/challenge-248/mark-anderson/raku/ch-2.raku b/challenge-248/mark-anderson/raku/ch-2.raku index 60784f7d98..6289177402 100644 --- a/challenge-248/mark-anderson/raku/ch-2.raku +++ b/challenge-248/mark-anderson/raku/ch-2.raku @@ -26,41 +26,41 @@ is-deeply submatrix-sum([ ]; is-deeply submatrix-sum([ - [ 22, 4, 20, 24 ], - [ 21, 16, 23, 18 ], - [ 3, 12, 19, 5 ], - [ 15, 7, 10, 2 ], - [ 17, 11, 6, 13 ], - [ 9, 8, 14, 1 ] + [22, 4, 20, 24], + [21, 16, 23, 18], + [ 3, 12, 19, 5], + [15, 7, 10, 2], + [17, 11, 6, 13], + [ 9, 8, 14, 1] ]), [ - [ 63, 63, 85 ], - [ 52, 70, 65 ], - [ 37, 48, 36 ], - [ 50, 34, 31 ], - [ 45, 39, 34 ] + [63, 63, 85], + [52, 70, 65], + [37, 48, 36], + [50, 34, 31], + [45, 39, 34] ]; is-deeply submatrix-sum([ - [ 26, 50, 10, 54, 42, 31, 45, 58 ], - [ 29, 64, 23, 35, 15, 41, 27, 17 ], - [ 55, 37, 1, 30, 52, 53, 21, 46 ], - [ 3, 2, 6, 36, 11, 13, 19, 22 ], - [ 61, 47, 32, 39, 49, 62, 9, 12 ], - [ 20, 60, 44, 7, 59, 34, 14, 16 ], - [ 8, 24, 38, 43, 56, 48, 57, 25 ], - [ 5, 40, 33, 28, 63, 18, 4, 51 ] + [26, 50, 10, 54, 42, 31, 45, 58], + [29, 64, 23, 35, 15, 41, 27, 17], + [55, 37, 1, 30, 52, 53, 21, 46], + [ 3, 2, 6, 36, 11, 13, 19, 22], + [61, 47, 32, 39, 49, 62, 9, 12], + [20, 60, 44, 7, 59, 34, 14, 16], + [ 8, 24, 38, 43, 56, 48, 57, 25], + [ 5, 40, 33, 28, 63, 18, 4, 51] ]), [ - [ 169, 147, 122, 146, 129, 144, 147 ], - [ 185, 125, 89, 132, 161, 142, 111 ], - [ 97, 46, 73, 129, 129, 106, 108 ], - [ 113, 87, 113, 135, 135, 103, 62 ], - [ 188, 183, 122, 154, 204, 119, 51 ], - [ 112, 166, 132, 165, 197, 153, 112 ], - [ 77, 135, 142, 190, 185, 127, 137 ] + [169, 147, 122, 146, 129, 144, 147], + [185, 125, 89, 132, 161, 142, 111], + [ 97, 46, 73, 129, 129, 106, 108], + [113, 87, 113, 135, 135, 103, 62], + [188, 183, 122, 154, 204, 119, 51], + [112, 166, 132, 165, 197, 153, 112], + [ 77, 135, 142, 190, 185, 127, 137] ]; sub submatrix-sum(@a) -- cgit From e3ad4ebb7065b755ab3e424cd207a58dc7cb23a7 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 18 Dec 2023 10:22:41 +0000 Subject: Challenge 248 Solutions (Raku) --- challenge-248/mark-anderson/raku/ch-1.raku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-248/mark-anderson/raku/ch-1.raku b/challenge-248/mark-anderson/raku/ch-1.raku index 9f9cfc21e3..9d664435ed 100644 --- a/challenge-248/mark-anderson/raku/ch-1.raku +++ b/challenge-248/mark-anderson/raku/ch-1.raku @@ -24,13 +24,13 @@ sub shortest-distance($str, $char) my $middle = gather for @a -> $a, $b { take 0 xx $a.chars; - take distance($b) + take distances($b) } flat ($head, $middle, $tail)>>.List } -sub distance($s) +sub distances($s) { return Empty unless $s; my $mid = ($s.chars / 2).ceiling; -- cgit From 44a3aff0801ce69831c7e3f78885b82bda423594 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 18 Dec 2023 12:54:07 +0000 Subject: w248 - Task 1 & 2 --- challenge-248/perlboy1967/perl/ch1.pl | 45 ++++++++++++++++++++++++++ challenge-248/perlboy1967/perl/ch2.pl | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100755 challenge-248/perlboy1967/perl/ch1.pl create mode 100755 challenge-248/perlboy1967/perl/ch2.pl diff --git a/challenge-248/perlboy1967/perl/ch1.pl b/challenge-248/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..f8936a8df4 --- /dev/null +++ b/challenge-248/perlboy1967/perl/ch1.pl @@ -0,0 +1,45 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 248 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-248 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Shortest Distance +Submitted by: Mohammad S Anwar + +You are given a string and a character in the given string. + +Write a script to return an array of integers of size same as length of the given string such that: + +distance[i] is the distance from index i to the closest occurence of +the given character in the given string. + +The distance between two indices i and j is abs(i - j). + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0; + +use List::MoreUtils qw(indexes); +use List::Util qw(min); + +sub shortestDistance($str,$ch) { + my @ch = split //, $str; + my @idx = indexes { $_ eq $ch } @ch; + + map { + my $i = $_; $ch[$_] eq $ch ? 0 : min(map { abs($i - $_) } @idx); + } 0 .. $#ch; +} + +is([shortestDistance('loveleetcode','e')],[3,2,1,0,1,0,0,1,2,2,1,0]); +is([shortestDistance('aaab','b')],[3,2,1,0]); + +done_testing; diff --git a/challenge-248/perlboy1967/perl/ch2.pl b/challenge-248/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..3e7dfe51cd --- /dev/null +++ b/challenge-248/perlboy1967/perl/ch2.pl @@ -0,0 +1,60 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 248 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-248 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Submatrix Sum +Submitted by: Jorg Sommrey + +You are given a NxM matrix A of integers. + +Write a script to construct a (N-1)x(M-1) matrix B having elements that are the +sum over the 2x2 submatrices of A, + +b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1] + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0; + +sub submatrixSum($arRef) { + map { + my $r = $_; + [map { + $arRef->[$r][$_] + + $arRef->[$r+1][$_] + + $arRef->[$r][$_+1] + + $arRef->[$r+1][$_+1] + } 0 .. scalar(@{$arRef->[$r]}) - 2]; + } 0 .. scalar(@$arRef) - 2; +} + +is([submatrixSum([ + [ 1, 2, 3, 4], + [ 5, 6, 7, 8], + [ 9,10,11,12], +])],[ + [14,18,22], + [30,34,38], +]); + +is([submatrixSum([ + [1,0,0,0], + [0,1,0,0], + [0,0,1,0], + [0,0,0,1], +])],[ + [2,1,0], + [1,2,1], + [0,1,2], +]); + +done_testing; -- cgit From e2a7867e1ab365b7bc2787f0d650c56e789a74d3 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 18 Dec 2023 13:24:11 +0000 Subject: - Added solutions by Bob Lied. - Added solutions by David Ferrone. - Added solutions by W. Luis Mochan. - Added solutions by Mark Anderson. - Added solutions by Niels van Dijke. --- challenge-248/eric-cheung/python/ch-1.py | 21 + challenge-248/eric-cheung/python/ch-2.py | 13 + challenge-248/perlboy1967/perl/ch-1.pl | 45 + challenge-248/perlboy1967/perl/ch-2.pl | 60 + challenge-248/perlboy1967/perl/ch1.pl | 45 - challenge-248/perlboy1967/perl/ch2.pl | 60 - stats/pwc-challenge-247.json | 499 +++++ stats/pwc-current.json | 496 +---- stats/pwc-language-breakdown-summary.json | 72 +- stats/pwc-language-breakdown.json | 3265 +++++++++++++++-------------- stats/pwc-leaders.json | 716 +++---- stats/pwc-summary-1-30.json | 36 +- stats/pwc-summary-121-150.json | 106 +- stats/pwc-summary-151-180.json | 60 +- stats/pwc-summary-181-210.json | 40 +- stats/pwc-summary-211-240.json | 42 +- stats/pwc-summary-241-270.json | 110 +- stats/pwc-summary-271-300.json | 52 +- stats/pwc-summary-301-330.json | 48 +- stats/pwc-summary-31-60.json | 56 +- stats/pwc-summary-61-90.json | 94 +- stats/pwc-summary-91-120.json | 42 +- stats/pwc-summary.json | 664 +++--- 23 files changed, 3413 insertions(+), 3229 deletions(-) create mode 100755 challenge-248/eric-cheung/python/ch-1.py create mode 100755 challenge-248/eric-cheung/python/ch-2.py create mode 100755 challenge-248/perlboy1967/perl/ch-1.pl create mode 100755 challenge-248/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-248/perlboy1967/perl/ch1.pl delete mode 100755 challenge-248/perlboy1967/perl/ch2.pl create mode 100644 stats/pwc-challenge-247.json diff --git a/challenge-248/eric-cheung/python/ch-1.py b/challenge-248/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..6a7dab3628 --- /dev/null +++ b/challenge-248/eric-cheung/python/ch-1.py @@ -0,0 +1,21 @@ + +## Example 1 +## strInput = "loveleetcode" +## strChar = "e" + +## Example 2 +strInput = "aaab" +strChar = "b" + +arrCharIndx = [nIndx for nIndx in range(len(strInput)) if strInput[nIndx] == strChar] +arrOutput = [] + +for nIndx in range(len(strInput)): + if strInput[nIndx] == strChar: + arrOutput.append(0) + elif nIndx < arrCharIndx[0]: + arrOutput.append(arrCharIndx[0] - nIndx) + else: + arrOutput.append(min([abs(nLoop - nIndx) for nLoop in arrCharIndx])) + +print (arrOutput) diff --git a/challenge-248/eric-cheung/python/ch-2.py b/challenge-248/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..61e8e84fd1 --- /dev/null +++ b/challenge-248/eric-cheung/python/ch-2.py @@ -0,0 +1,13 @@ + +## arrMatrixInput = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] ## Example 1 +arrMatrixInput = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] ## Example 2 + +arrMatrixOutput = [] + +for nRow in range(len(arrMatrixInput) - 1): + arrRow = [] + for nCol in range(len(arrMatrixInput[nRow]) - 1): + arrRow.append(arrMatrixInput[nRow][nCol] + arrMatrixInput[nRow][nCol + 1] + arrMatrixInput[nRow + 1][nCol] + arrMatrixInput[nRow + 1][nCol + 1]) + arrMatrixOutput.append(arrRow) + +print (arrMatrixOutput) diff --git a/challenge-248/perlboy1967/perl/ch-1.pl b/challenge-248/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..f8936a8df4 --- /dev/null +++ b/challenge-248/perlboy1967/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 248 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-248 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Shortest Distance +Submitted by: Mohammad S Anwar + +You are given a string and a character in the given string. + +Write a script to return an array of integers of size same as length of the given string such that: + +distance[i] is the distance from index i to the closest occurence of +the given character in the given string. + +The distance between two indices i and j is abs(i - j). + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0; + +use List::MoreUtils qw(indexes); +use List::Util qw(min); + +sub shortestDistance($str,$ch) { + my @ch = split //, $str; + my @idx = indexes { $_ eq $ch } @ch; + + map { + my $i = $_; $ch[$_] eq $ch ? 0 : min(map { abs($i - $_) } @idx); + } 0 .. $#ch; +} + +is([shortestDistance('loveleetcode','e')],[3,2,1,0,1,0,0,1,2,2,1,0]); +is([shortestDistance('aaab','b')],[3,2,1,0]); + +done_testing; diff --git a/challenge-248/perlboy1967/perl/ch-2.pl b/challenge-248/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..3e7dfe51cd --- /dev/null +++ b/challenge-248/perlboy1967/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 248 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-248 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Submatrix Sum +Submitted by: Jorg Sommrey + +You are given a NxM matrix A of integers. + +Write a script to construct a (N-1)x(M-1) matrix B having elements that are the +sum over the 2x2 submatrices of A, + +b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1] + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Test2::V0; + +sub submatrixSum($arRef) { + map { + my $r = $_; + [map { + $arRef->[$r][$_] + + $arRef->[$r+1][$_] + + $arRef->[$r][$_+1] + + $arRef->[$r+1][$_+1] + } 0 .. scalar(@{$arRef->[$r]}) - 2]; + } 0 .. scalar(@$arRef) - 2; +} + +is([submatrixSum([ + [ 1, 2, 3, 4], + [ 5, 6, 7, 8], + [ 9,10,11,12], +])],[ + [14,18,22], + [30,34,38], +]); + +is([submatrixSum([ + [1,0,0,0], + [0,1,0,0], + [0,0,1,0], + [0,0,0,1], +])],[ + [2,1,0], + [1,2,1], + [0,1,2], +]); + +done_testing; diff --git a/challenge-248/perlboy1967/perl/ch1.pl b/challenge-248/perlboy1967/perl/ch1.pl deleted file mode 100755 index f8936a8df4..0000000000 --- a/challenge-248/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,45 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 248 -- https://theweeklychallenge.org/blog/perl-weekly-challenge-248 - -Author: Niels 'PerlBoy' van Dijke - -Task 1: Shortest Distance -Submitted by: Mohammad S Anwar - -You are given a string and a character in the given string. - -Write a script to return an array of integers of size same as length of the given string such that: - -distance[i] is the distance from index i to the closest occurence of -the given character in the given string. - -The distance between two indices i and j is abs(i - j). - -=cut - -use v5.32; -use common::sense; -use feature qw(signatures); - -use Test2::V0; - -use List::MoreUtils qw(indexes); -use List::Util qw(min); - -sub shortestDistance($str,$ch) { - my @ch = split //, $str; - my @idx = indexes { $_ eq $ch } @ch; - - map { - my $i = $_; $ch[$_] eq $ch ? 0 : min(map { abs($i - $_) } @idx); - } 0 .. $#ch; -} - -is([shortestDistance('loveleetcode','e')],[3,2,1,0,1,0,0,1,2,2,1,0]); -is([shortestDistance('aaab','b')],[3,2,1,0]); - -done_testing; diff --git a/challenge-248/perlboy1967/perl/ch2.pl b/challenge-248/perlboy1967/perl/ch2.pl deleted file mode 100755 index 3e7dfe51cd..0000000000 --- a/challenge-248/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,60 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 248 -- https://theweeklychallenge.org/blog/perl-weekly-challenge-248 - -Author: Niels 'PerlBoy' van Dijke - -Task 2: Submatrix Sum -Submitted by: Jorg Sommrey - -You are given a NxM matrix A of integers. - -Write a script to construct a (N-1)x(M-1) matrix B having elements that are the -sum over the 2x2 submatrices of A, - -b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1] - -=cut - -use v5.32; -use common::sense; -use feature qw(signatures); - -use Test2::V0; - -sub submatrixSum($arRef) { - map { - my $r = $_; - [map { - $arRef->[$r][$_] + - $arRef->[$r+1][$_] + - $arRef->[$r][$_+1] + - $arRef->[$r+1][$_+1] - } 0 .. scalar(@{$arRef->[$r]}) - 2]; - } 0 .. scalar(@$arRef) - 2; -} - -is([submatrixSum([ - [ 1, 2, 3, 4], - [ 5, 6, 7, 8], - [ 9,10,11,12], -])],[ - [14,18,22], - [30,34,38], -]); - -is([submatrixSum([ - [1,0,0,0], - [0,1,0,0], - [0,0,1,0], - [0,0,0,1], -])],[ - [2,1,0], - [1,2,1], - [0,1,2], -]); - -done_testing; diff --git a/stats/pwc-challenge-247.json b/stats/pwc-challenge-247.json new file mode 100644 index 0000000000..aedb2cfcef --- /dev/null +++ b/stats/pwc-challenge-247.json @@ -0,0 +1,499 @@ +{ + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "xAxis" : { + "type" : "category" + }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "format" : "{point.y}", + "enabled" : 1 + }, + "borderWidth" : 0 + } + }, + "title" : { + "text" : "The Weekly Challenge - 247" + }, + "series" : [ + { + "data" : [ + { + "name" : "Arne Sommer", + "drilldown" : "Arne Sommer", + "y" : 3 + }, + { + "y" : 2, + "drilldown" : "Athanasius", + "name" : "Athanasius" + }, + { + "name" : "BarrOff", + "drilldown" : "BarrOff", + "y" : 2 + }, + { + "y" : 2, + "name" : "Bob Lied", + "drilldown" : "Bob Lied" + }, + { + "y" : 2, + "drilldown" : "Bruce Gray", + "name" : "Bruce Gray" + }, + { + "y" : 1, + "drilldown" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung" + }, + { + "name" : "David Ferrone", + "drilldown" : "David Ferrone", + "y" : 2 + }, + { + "name" : "E. Choroba", + "drilldown" : "E. Choroba", + "y" : 2 + }, + { + "y" : 3, + "drilldown" : "Jorg Sommrey", + "name" : "Jorg Sommrey" + }, + { + "y" : 3, + "name" : "Laurent Rosenfeld", + "drilldown" : "Laurent Rosenfeld" + }, + { + "drilldown" : "Lubos Kolouch", + "name" : "Lubos Kolouch", + "y" : 5 + }, + { + "y" : 10, + "drilldown" : "Luca Ferrari", + "name" : "Luca Ferrari" + }, + { + "drilldown" : "Mark Anderson", + "name" : "Mark Anderson", + "y" : 2 + }, + { + "name" : "Matthew Neleigh", + "drilldown" : "Matthew Neleigh", + "y" : 2 + }, + { + "drilldown" : "Nelo Tovar", + "name" : "Nelo Tovar", + "y" : 2 + }, + { + "y" : 2, + "name" : "Niels van Dijke", + "drilldown" : "Niels van Dijke" + }, + { + "name" : "Packy Anderson", + "drilldown" : "Packy Anderson", + "y" : 5 + }, + { + "drilldown" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith", + "y" : 3 + }, + { + "y" : 2, + "drilldown" : "Peter Meszaros", + "name" : "Peter Meszaros" + }, + { + "y" : 5, + "drilldown" : "Roger Bell_West", + "name" : "Roger Bell_West" + }, + { + "y" : 3, + "name" : "Simon Green", + "drilldown" : "Simon Green" + }, + { + "y" : 3, + "name" : "Stephen G. Lynn", + "drilldown" : "Stephen G. Lynn" + }, + { + "y" : 4, + "name" : "Thomas Kohler", + "drilldown" : "Thomas Kohler" + }, + { + "y" : 4, + "drilldown" : "Ulrich Rieke", + "name" : "Ulrich Rieke" + }, + { + "drilldown" : "W. Luis Mochan", + "name" : "W. Luis Mochan", + "y" : 3 + } + ], + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 247" + } + ], + "drilldown" : { + "series" : [ + { + "name" : "Arne Sommer", + "id" : "Arne Sommer", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "id" : "Athanasius", + "name" : "Athanasius", + "data" : [ + [ + "Perl", + 1 + ], + [ + "Raku", + 1 + ] + ] + }, + { + "id" : "BarrOff", + "name" : "BarrOff", + "data" : [ + [ + "Perl", + 1 + ], + [ + "Raku", + 1 + ] + ] + }, + { + "id" : "Bob Lied", + "name" : "Bob Lied", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "id" : "Bruce Gray", + "name" : "Bruce Gray", + "data" : [ + [ + "Raku", + 2 + ] + ] + }, + { + "name" : "Cheok-Yin Fung", + "id" : "Cheok-Yin Fung", + "data" : [ + [ + "Perl", + 1 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "David Ferrone", + "id" : "David Ferrone" + }, + { + "name" : "E. Choroba", + "id" : "E. Choroba", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "id" : "Jorg Sommrey", + "name" : "Jorg Sommrey", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "id" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld", + "data" : [ + [ + "Perl", + 1 + ], + [ + "Raku", + 1 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Lubos Kolouch", + "name" : "Lubos Kolouch" + }, + { + "id" : "Luca Ferrari", + "name" : "Luca Ferrari", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 8 + ] + ] + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Mark Anderson", + "name" : "Mark Anderson" + }, + { + "name" : "Matthew Neleigh", + "id" : "Matthew Neleigh", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "Nelo Tovar", + "id" : "Nelo Tovar" + }, + { + "name" : "Niels van Dijke", + "id" : "Niels van Dijke", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Packy Anderson", + "id" : "Packy Anderson" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Peter Campbell Smith", + "name" : "Peter Campbell Smith" + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Peter Meszaros", + "name" : "Peter Meszaros" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Roger Bell_West", + "name" : "Roger Bell_West" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Simon Green", + "id" : "Simon Green" + }, + { + "name" : "Stephen G. Lynn", + "i