From a3f5ca2bc4fa1b216a9d1b1097aaf9af0a269aa4 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Fri, 22 Dec 2023 11:19:20 -0500 Subject: TWC248 --- challenge-248/deadmarshal/blog.txt | 1 + challenge-248/deadmarshal/perl/ch-1.pl | 14 ++++++++++++++ challenge-248/deadmarshal/perl/ch-2.pl | 21 +++++++++++++++++++++ challenge-248/deadmarshal/raku/ch-1.raku | 11 +++++++++++ challenge-248/deadmarshal/raku/ch-2.raku | 17 +++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 challenge-248/deadmarshal/blog.txt create mode 100644 challenge-248/deadmarshal/perl/ch-1.pl create mode 100644 challenge-248/deadmarshal/perl/ch-2.pl create mode 100644 challenge-248/deadmarshal/raku/ch-1.raku create mode 100644 challenge-248/deadmarshal/raku/ch-2.raku diff --git a/challenge-248/deadmarshal/blog.txt b/challenge-248/deadmarshal/blog.txt new file mode 100644 index 0000000000..a553152768 --- /dev/null +++ b/challenge-248/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2023/12/twc248.html diff --git a/challenge-248/deadmarshal/perl/ch-1.pl b/challenge-248/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..2844d9fcd6 --- /dev/null +++ b/challenge-248/deadmarshal/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::MoreUtils qw(indexes); +use List::Util qw(min); + +sub shortest_distance{ + my @arr = split'',$_[0]; + map{my $i = $_; min map{abs $i - $_}indexes{$_ eq $_[1]}@arr}0..$#arr; +} + +printf "(%s)\n",join ',',shortest_distance('loveleetcode','e'); +printf "(%s)\n",join ',',shortest_distance('aaab','b'); + diff --git a/challenge-248/deadmarshal/perl/ch-2.pl b/challenge-248/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..c6af446383 --- /dev/null +++ b/challenge-248/deadmarshal/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Data::Show; + +sub submatrix_sum{ + my ($m) = @_; + my @ret; + foreach my $i(0..$#$m-1){ + push @ret,[]; + foreach my $j(0..$#{$m->[0]}-1){ + $ret[$i][$j] = $m->[$i][$j] + $m->[$i][$j+1] + + $m->[$i+1][$j] + $m->[$i+1][$j+1] + } + } + @ret +} + +print show submatrix_sum([[1,2,3,4],[5,6,7,8],[9,10,11,12]]); +print show submatrix_sum([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]); + diff --git a/challenge-248/deadmarshal/raku/ch-1.raku b/challenge-248/deadmarshal/raku/ch-1.raku new file mode 100644 index 0000000000..86eec1e961 --- /dev/null +++ b/challenge-248/deadmarshal/raku/ch-1.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku + +sub shortest-distance($str,$c) +{ + my @arr = $str.comb; + (0..@arr.end).map: {my $i = $_; ($str.indices($c).map: {abs $i - $_}).min} +} + +say shortest-distance('loveleetcode','e'); +say shortest-distance('aaab','b'); + diff --git a/challenge-248/deadmarshal/raku/ch-2.raku b/challenge-248/deadmarshal/raku/ch-2.raku new file mode 100644 index 0000000000..faced1fe47 --- /dev/null +++ b/challenge-248/deadmarshal/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku + +sub submatrix-sum(@m) +{ + my @ret; + for 0..@m.elems-2 -> $i { + for 0..@m[0].elems-2 -> $j { + @ret[$i;$j] = @m[$i;$j] + @m[$i;$j+1] + + @m[$i+1;$j] + @m[$i+1;$j+1]; + } + } + @ret +} + +say submatrix-sum([[1,2,3,4],[5,6,7,8],[9,10,11,12]]); +say submatrix-sum([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]); + -- cgit