aboutsummaryrefslogtreecommitdiff
path: root/challenge-248
diff options
context:
space:
mode:
authorJoelle Maslak <jmaslak@antelope.net>2023-12-20 19:53:33 -0700
committerGitHub <noreply@github.com>2023-12-20 19:53:33 -0700
commitac10d33ad4be2763bba001150bef6e838d017546 (patch)
treea465bedb1a1bbbe6380c3c169404bf4565b893c3 /challenge-248
parent80750c8e164c159fe869b652be98a1eef64373dd (diff)
parent33f4358de5040ddc31b53689b6a781efbc2847c1 (diff)
downloadperlweeklychallenge-club-ac10d33ad4be2763bba001150bef6e838d017546.tar.gz
perlweeklychallenge-club-ac10d33ad4be2763bba001150bef6e838d017546.tar.bz2
perlweeklychallenge-club-ac10d33ad4be2763bba001150bef6e838d017546.zip
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-248')
-rw-r--r--challenge-248/bob-lied/README6
-rw-r--r--challenge-248/bob-lied/perl/ch-1.pl141
-rw-r--r--challenge-248/bob-lied/perl/ch-2.pl75
-rw-r--r--challenge-248/dave-jacoby/blog.txt1
-rw-r--r--challenge-248/dave-jacoby/perl/ch-1.pl41
-rw-r--r--challenge-248/dave-jacoby/perl/ch-2.pl59
-rwxr-xr-xchallenge-248/e-choroba/perl/ch-1.pl26
-rwxr-xr-xchallenge-248/e-choroba/perl/ch-2.pl38
-rwxr-xr-xchallenge-248/eric-cheung/python/ch-1.py21
-rwxr-xr-xchallenge-248/eric-cheung/python/ch-2.py13
-rw-r--r--challenge-248/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-248/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-248/jeanluc2020/perl/ch-1.pl70
-rwxr-xr-xchallenge-248/jeanluc2020/perl/ch-2.pl74
-rwxr-xr-xchallenge-248/jeanluc2020/python/ch-1.py66
-rwxr-xr-xchallenge-248/jeanluc2020/python/ch-2.py71
-rw-r--r--challenge-248/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-248/laurent-rosenfeld/perl/ch-1.pl26
-rw-r--r--challenge-248/laurent-rosenfeld/raku/ch-1.raku22
-rw-r--r--challenge-248/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-7.txt1
-rw-r--r--challenge-248/luca-ferrari/blog-8.txt1
-rw-r--r--challenge-248/luca-ferrari/postgresql/ch-1.plperl52
-rw-r--r--challenge-248/luca-ferrari/postgresql/ch-1.sql31
-rw-r--r--challenge-248/luca-ferrari/postgresql/ch-2.plperl28
-rw-r--r--challenge-248/luca-ferrari/postgresql/ch-2.sql32
-rw-r--r--challenge-248/luca-ferrari/python/ch-1.py37
-rw-r--r--challenge-248/luca-ferrari/python/ch-2.py42
-rw-r--r--challenge-248/luca-ferrari/raku/ch-1.p624
-rw-r--r--challenge-248/luca-ferrari/raku/ch-2.p627
-rw-r--r--challenge-248/mark-anderson/raku/ch-1.raku46
-rw-r--r--challenge-248/mark-anderson/raku/ch-2.raku72
-rwxr-xr-xchallenge-248/mattneleigh/perl/ch-1.pl95
-rwxr-xr-xchallenge-248/mattneleigh/perl/ch-2.pl176
-rwxr-xr-xchallenge-248/perlboy1967/perl/ch-1.pl45
-rwxr-xr-xchallenge-248/perlboy1967/perl/ch-2.pl60
-rw-r--r--challenge-248/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-248/peter-campbell-smith/perl/ch-1.pl44
-rwxr-xr-xchallenge-248/peter-campbell-smith/perl/ch-2.pl57
-rwxr-xr-xchallenge-248/peter-meszaros/perl/ch-1.pl102
-rwxr-xr-xchallenge-248/peter-meszaros/perl/ch-2.pl80
-rw-r--r--challenge-248/pokgopun/go/ch-1.go95
-rw-r--r--challenge-248/pokgopun/go/ch-2.go109
-rw-r--r--challenge-248/pokgopun/python/ch-1.py59
-rw-r--r--challenge-248/pokgopun/python/ch-2.py91
-rwxr-xr-xchallenge-248/roger-bell-west/javascript/ch-1.js72
-rwxr-xr-xchallenge-248/roger-bell-west/javascript/ch-2.js61
-rw-r--r--challenge-248/roger-bell-west/kotlin/ch-1.kt47
-rw-r--r--challenge-248/roger-bell-west/kotlin/ch-2.kt34
-rwxr-xr-xchallenge-248/roger-bell-west/lua/ch-1.lua77
-rwxr-xr-xchallenge-248/roger-bell-west/lua/ch-2.lua63
-rwxr-xr-xchallenge-248/roger-bell-west/perl/ch-1.pl39
-rwxr-xr-xchallenge-248/roger-bell-west/perl/ch-2.pl28
-rw-r--r--challenge-248/roger-bell-west/postscript/ch-1.ps179
-rw-r--r--challenge-248/roger-bell-west/postscript/ch-2.ps125
-rwxr-xr-xchallenge-248/roger-bell-west/python/ch-1.py37
-rwxr-xr-xchallenge-248/roger-bell-west/python/ch-2.py26
-rwxr-xr-xchallenge-248/roger-bell-west/raku/ch-1.p636
-rwxr-xr-xchallenge-248/roger-bell-west/raku/ch-2.p626
-rwxr-xr-xchallenge-248/roger-bell-west/ruby/ch-1.rb44
-rwxr-xr-xchallenge-248/roger-bell-west/ruby/ch-2.rb34
-rwxr-xr-xchallenge-248/roger-bell-west/rust/ch-1.rs36
-rwxr-xr-xchallenge-248/roger-bell-west/rust/ch-2.rs30
-rw-r--r--challenge-248/roger-bell-west/scala/ch-1.scala48
-rw-r--r--challenge-248/roger-bell-west/scala/ch-2.scala35
-rw-r--r--challenge-248/roger-bell-west/tests.yaml78
-rwxr-xr-xchallenge-248/ulrich-rieke/cpp/ch-1.cpp52
-rwxr-xr-xchallenge-248/ulrich-rieke/cpp/ch-2.cpp59
-rwxr-xr-xchallenge-248/ulrich-rieke/haskell/ch-1.hs17
-rwxr-xr-xchallenge-248/ulrich-rieke/haskell/ch-2.hs41
-rwxr-xr-xchallenge-248/ulrich-rieke/perl/ch-1.pl34
-rwxr-xr-xchallenge-248/ulrich-rieke/perl/ch-2.pl34
-rwxr-xr-xchallenge-248/ulrich-rieke/raku/ch-1.raku28
-rwxr-xr-xchallenge-248/ulrich-rieke/raku/ch-2.raku29
-rwxr-xr-xchallenge-248/ulrich-rieke/rust/ch-1.rs31
-rwxr-xr-xchallenge-248/ulrich-rieke/rust/ch-2.rs42
-rw-r--r--challenge-248/wlmb/blog.txt1
-rwxr-xr-xchallenge-248/wlmb/perl/ch-1.pl15
-rwxr-xr-xchallenge-248/wlmb/perl/ch-2.pl16
-rw-r--r--challenge-248/zapwai/perl/ch-1.pl19
-rw-r--r--challenge-248/zapwai/perl/ch-2.pl28
86 files changed, 3764 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
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..e202a54191
--- /dev/null
+++ b/challenge-248/bob-lied/perl/ch-1.pl
@@ -0,0 +1,141 @@
+#!/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 ceil floor/; 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;
+
+ # 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
+ # vvvvvvvvvvvvvvvvvvvvvvvvvvvv
+ push @dist, min map { abs($_ - $i) } @cloc;
+ }
+ 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;
+ 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;
+}
+
+# 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)), reverse( 0 .. floor($diff/2));
+ $loc = $next;
+ }
+ if ( $loc < $#s )
+ {
+ push @dist, 1 .. ($#s - $loc);
+ }
+ 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");
+
+ 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( sd3("ab", 'x'), [undef, undef], "sd3 no x in str");
+ is( sd3("", 'x'), [], "sd3 empty string");
+
+ done_testing;
+}
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;
+}
diff --git a/challenge-248/dave-jacoby/blog.txt b/challenge-248/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..fa7d7dd4cf
--- /dev/null
+++ b/challenge-248/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2023/12/19/tumbling-down-the-rabbit-hole-weekly-challenge-248.html
diff --git a/challenge-248/dave-jacoby/perl/ch-1.pl b/challenge-248/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..9bc88c0a8e
--- /dev/null
+++ b/challenge-248/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use List::Util qw{ min };
+
+my @examples = (
+
+ { str => "loveleetcode", char => "e" },
+ { str => "aaab", char => "b" },
+
+);
+
+for my $example (@examples) {
+ my @output = shortest_distance($example);
+ my $output = join ',', @output;
+
+ say <<~"END";
+ Input: \$str = "$example->{str}", \$char = "$example->{char}"
+ Output: ($output)
+ END
+
+}
+
+# 1) everybody gets matched
+# 2) nobody gets matched to themself
+sub shortest_distance ($input) {
+ my $str = $input->{str};
+ my $char = $input->{char};
+ my @input = grep { $char eq substr $str, $_, 1 } 0 .. length $str;
+ my @output;
+
+ for my $i ( 0 .. -1 + length $str ) {
+ my $c = substr $str, $i, 1;
+ push @output, min map { abs $_ - $i } @input;
+ }
+
+ return @output;
+}
diff --git a/challenge-248/dave-jacoby/perl/ch-2.pl b/challenge-248/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..f0e0a58cc4
--- /dev/null
+++ b/challenge-248/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use List::Util qw{ max sum };
+
+my @examples = (
+ [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ],
+ [ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ]
+);
+
+for my $e (@examples) {
+ my $o = submatrix_sum($e);
+ my $input = format_matrix($e);
+ my $output = format_matrix($o);
+<