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 (limited to 'challenge-248') 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(-) (limited to 'challenge-248') 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 (limited to 'challenge-248') 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(+) (limited to 'challenge-248') 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(-) (limited to 'challenge-248') 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(-) (limited to 'challenge-248') 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 (limited to 'challenge-248') 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 (limited to 'challenge-248') 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 (limited to 'challenge-248') 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 (limited to 'challenge-248') 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(-) (limited to 'challenge-248') 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(-) (limited to 'challenge-248') 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 (limited to 'challenge-248') 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 -------------------------------- 6 files changed, 139 insertions(+), 105 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 (limited to 'challenge-248') 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; -- cgit From 74ac2656326d8e5d1dcd9c482036ac9ec2c606aa Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 18 Dec 2023 14:38:20 +0000 Subject: Week 248 ... --- challenge-248/peter-campbell-smith/blog.txt | 1 + challenge-248/peter-campbell-smith/perl/ch-1.pl | 44 +++++++++++++++++++ challenge-248/peter-campbell-smith/perl/ch-2.pl | 57 +++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 challenge-248/peter-campbell-smith/blog.txt create mode 100755 challenge-248/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-248/peter-campbell-smith/perl/ch-2.pl (limited to 'challenge-248') diff --git a/challenge-248/peter-campbell-smith/blog.txt b/challenge-248/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..a03a6fdaef --- /dev/null +++ b/challenge-248/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/248 diff --git a/challenge-248/peter-campbell-smith/perl/ch-1.pl b/challenge-248/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..da6e352ff5 --- /dev/null +++ b/challenge-248/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +use v5.26; # The Weekly Challenge - 2023-12-18 +use utf8; # Week 248 task 1 - Shortest distance +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +shortest_distance('loveleetcode', 'e'); +shortest_distance('We wish you a merry christmas and a happy new year!', 'a'); +shortest_distance('zoos contain animals', 'z'); +shortest_distance('tornadoes have a vortex', 'x'); +shortest_distance('xylophones and zithers', 'q'); + +sub shortest_distance { + + my ($string, $char, @letters, $last, @count, @dist, $j, $count, $i, $k); + + ($string, $char) = @_; + + # initialise + @letters = split('', $string); + $last = @letters - 1; + $dist[$_] = 999 for 0 .. $last; + $count = 999; + + # go along string forwards and backwards + for $k (0, 1) { + for $j (0 .. $last) { + $i = $k ? $j : $last - $j; + + # if we've found a $char reset count to 0 + if ($letters[$i] eq $char) { + $count = $dist[$i] = 0; + + # or save the distance to the nearest $char + } else { + $count ++; + $dist[$i] = $count if $count < $dist[$i]; + } + } + } + say qq[\nInput: \@string = '$string', \$char = '$char']; + say qq[Output: ] . ($dist[0] != 999 ? join(', ', @dist) : qq['$char' does not occur in '$string']); +} diff --git a/challenge-248/peter-campbell-smith/perl/ch-2.pl b/challenge-248/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..eecc457542 --- /dev/null +++ b/challenge-248/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-12-18 +use utf8; # Week 242 task 2 - Submatrix sum +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +submatrix_sum([1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12]); +submatrix_sum([37, 99, 43, 87, 22, 77, 0, 51, 20], + [18, 5, 67, 39, 52, 40, 39, 77, 1], + [93, 45, 34, 87, 12, 34, 15, 90, 22], + [11, 84, 45, 36, 75, 83, 40, 58, 89]); + +sub submatrix_sum { + + my ($in, $cols, $rows, $c, $r, $out, $chars, $max); + + # initialise + $in = \@_; + $cols = @{$in->[0]} - 1; + $rows = @$in - 1; + $max = 0; + + # loop over rows and columns calculating sum + for $r (0 .. $rows - 1) { + for $c (0 .. $cols - 1) { + $out->[$r]->[$c] = $in->[$r]->[$c] + $in->[$r + 1]->[$c] + + $in->[$r]->[$c + 1] + $in->[$r + 1]->[$c + 1]; + $chars = length($out->[$r]->[$c]); + $max = $chars if $chars > $max; + } + } + + # output the required data + print_matrix('Input: [', $in, $max); + print_matrix('Output: [', $out, $max); +} + + +sub print_matrix { + + my ($legend, $matrix, $j, $out, $max); + + ($legend, $matrix, $max) = @_; + + # format rows of matrix with numbers of equal width + $out = ''; + for $j (0 .. @$matrix - 1) { + $out .= qq[$legend] . join(', ', @{$matrix->[$j]}) . qq(]\n); + $legend = ' ['; + } + $out =~ s|(\d+)|sprintf("%${max}d", $1)|ge; + say $out; +} + \ No newline at end of file -- cgit From 6d2834f8e078f16934ff9cb34bf8cc5d2d0f6189 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Tue, 19 Dec 2023 03:15:11 +1100 Subject: pwc248 solution in python --- challenge-248/pokgopun/python/':q | 59 +++++++++++++++++++++++ challenge-248/pokgopun/python/ch-1.py | 59 +++++++++++++++++++++++ challenge-248/pokgopun/python/ch-2.py | 91 +++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 challenge-248/pokgopun/python/':q create mode 100644 challenge-248/pokgopun/python/ch-1.py create mode 100644 challenge-248/pokgopun/python/ch-2.py (limited to 'challenge-248') diff --git a/challenge-248/pokgopun/python/':q b/challenge-248/pokgopun/python/':q new file mode 100644 index 0000000000..196199331d --- /dev/null +++ b/challenge-248/pokgopun/python/':q @@ -0,0 +1,59 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-248/ +""" + +Task 1: Shortest Distance + +Submitted by: [58]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). + +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 the '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) + +Task 2: Submatrix Sum +""" +### solution by pokgopun@gmail.com + +def shortestDistance(word, letter): + l = len(word) + letterIndexes = tuple( i for i in range(l) if word[i]==letter ) + return tuple( + min( abs(x-i) for i in letterIndexes ) for x in range(l) + ) + +import unittest + +class TestShortestDistance(unittest.TestCase): + def test(self): + for (word,letter),distance in { + ("loveleetcode", "e"): (3,2,1,0,1,0,0,1,2,2,1,1), + ("aaab", "b"): (3,2,1,0), + }.items(): + self.assertEqual(shortestDistance(word,letter),distance) + +unittest.main() diff --git a/challenge-248/pokgopun/python/ch-1.py b/challenge-248/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..edb003520b --- /dev/null +++ b/challenge-248/pokgopun/python/ch-1.py @@ -0,0 +1,59 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-248/ +""" + +Task 1: Shortest Distance + +Submitted by: [58]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). + +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 the '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) + +Task 2: Submatrix Sum +""" +### solution by pokgopun@gmail.com + +def shortestDistance(word, letter): + l = len(word) + letterIndexes = tuple( i for i in range(l) if word[i]==letter ) + return tuple( + min( abs(x-i) for i in letterIndexes ) for x in range(l) + ) + +import unittest + +class TestShortestDistance(unittest.TestCase): + def test(self): + for (word,letter),distance in { + ("loveleetcode", "e"): (3,2,1,0,1,0,0,1,2,2,1,0), + ("aaab", "b"): (3,2,1,0), + }.items(): + self.assertEqual(shortestDistance(word,letter),distance) + +unittest.main() diff --git a/challenge-248/pokgopun/python/ch-2.py b/challenge-248/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..96572b19a3 --- /dev/null +++ b/challenge-248/pokgopun/python/ch-2.py @@ -0,0 +1,91 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-248/ +""" + +Task 2: Submatrix Sum + +Submitted by: [59]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] + +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] + ] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 24th December + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def submatrixSum(tup: tuple): + d = len(tup) + l = len(tup[0]) + stup = tuple() + for y in range(d-1): + t = tuple() + for x in range(l-1): + t += ( sum( sum(tup[z][x:x+2]) for z in range(y,y+2) ), ) + stup += (t,) + return stup + +import unittest + +class TestSubmatrixSum(unittest.TestCase): + def test(self): + for inpt,otpt in { + ( + (1, 2, 3, 4), + (5, 6, 7, 8), + (9, 10, 11, 12), + ): ( + (14, 18, 22), + (30, 34, 38), + ), + ( + (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), + ), + }.items(): + self.assertEqual(submatrixSum(inpt),otpt) + +unittest.main() + + -- cgit From 78fb6f2659b09adf85db158916c357d5fbdd8e82 Mon Sep 17 00:00:00 2001 From: pme Date: Mon, 18 Dec 2023 18:15:56 +0100 Subject: challenge-248 --- challenge-248/peter-meszaros/perl/ch-1.pl | 102 ++++++++++++++++++++++++++++++ challenge-248/peter-meszaros/perl/ch-2.pl | 80 +++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100755 challenge-248/peter-meszaros/perl/ch-1.pl create mode 100755 challenge-248/peter-meszaros/perl/ch-2.pl (limited to 'challenge-248') diff --git a/challenge-248/peter-meszaros/perl/ch-1.pl b/challenge-248/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..818e50ff05 --- /dev/null +++ b/challenge-248/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,102 @@ +#!/usr/bin/env perl +# +# 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 the '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 strict; +use warnings; +use Test::More; +use Data::Dumper; + +my $cases = [ + [ "loveleetcode", "e"], + [ "aaab", "b"], +]; + +sub shortest_distance +{ + sub get_pos + { + my $pos = shift; + my $n = shift; + + my @res; + for (0..$#$pos) { + if ($_ == 0 && $n < $pos->[$_]) { # smaller than zeroth item + @res = (undef, $pos->[$_]); + last; + } elsif ($_ == $#$pos && $n > $pos->[$_]) { # bigger than last item + @res = ($pos->[$_], undef); + last; + } elsif ($n < $pos->[$_] && $n > $pos->[$_-1]) { + @res = ($pos->[$_-1], $pos->[$_]); + last; + } + } + return @res; + } + + my $str = $_[0]->[0]; + my $chr = $_[0]->[1]; + + my @str = split('', $str); + + # get chr positions + my @pos; + for (0..$#str) { + push @pos, $_ if $str[$_] eq $chr; + } + @pos = sort {$a <=> $b} @pos; + + my @ret; + for my $n (0..$#str) { + my $d = 0; + + # calculate distances + if ($str[$n] ne $chr) { + my @p = get_pos(\@pos, $n); + + if (defined $p[0]) { + $d = abs($p[0] - $n); + } + if (defined $p[1]) { + my $d2 = abs($p[1] - $n); + $d = $d2 if $d == 0 or $d2 < $d; + } + } + push @ret, $d;