aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-22 16:37:18 +0000
committerGitHub <noreply@github.com>2023-12-22 16:37:18 +0000
commit8d0ab22a35a0f43c074b7eff46a37e43cc9ea49a (patch)
tree56e75c365636288566141f99f80aaab8f56d3c5c
parent3f5af011f6ad71136c9a7c46dcdbc23254f2e7c7 (diff)
parentc1a1dda4abeaf8a610dc99cfa37c504b7f617b6d (diff)
downloadperlweeklychallenge-club-8d0ab22a35a0f43c074b7eff46a37e43cc9ea49a.tar.gz
perlweeklychallenge-club-8d0ab22a35a0f43c074b7eff46a37e43cc9ea49a.tar.bz2
perlweeklychallenge-club-8d0ab22a35a0f43c074b7eff46a37e43cc9ea49a.zip
Merge pull request #9273 from ntovar/branch-248
Challenge 247, Perl and Bash (ch-1) solutions. By Nelo Tovar
-rwxr-xr-xchallenge-248/nelo-tovar/bash/ch-1.sh54
-rw-r--r--challenge-248/nelo-tovar/perl/ch-1.pl47
-rw-r--r--challenge-248/nelo-tovar/perl/ch-2.pl61
3 files changed, 162 insertions, 0 deletions
diff --git a/challenge-248/nelo-tovar/bash/ch-1.sh b/challenge-248/nelo-tovar/bash/ch-1.sh
new file mode 100755
index 0000000000..a85ab7138b
--- /dev/null
+++ b/challenge-248/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,54 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 248 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-248/
+#
+# Task 1 : Shortes Distance
+
+function shortes_distance() {
+# local nums=("$@")
+# local length=${#nums[@]}
+ local str=$1
+ local length=${#str}
+ local chr=$2
+ local indexes=()
+ local distances=()
+
+ for (( i = 0; i < $length; i++ )); do
+ if [[ ${str:$i:1} == $chr ]]; then
+ indexes+=($i)
+ fi
+ done
+ for (( i = 0; i < $length; i++ )); do
+ min=$length
+
+ for j in ${indexes[@]}; do
+ d=$(($j - $i))
+ d=${d#-}
+
+ if [[ $d -lt $min ]]; then
+ min=$d
+ fi
+ done
+
+ distances+=($min)
+ done
+
+ echo ${distances[@]}
+}
+
+example_str_1='loveleetcode'
+example_str_2='aaab'
+example_chr_1='e'
+example_chr_2='b'
+
+for e in 1 2; do
+ str="example_str_$e"
+ chr="example_chr_$e"
+ sd=($(shortes_distance ${!str} ${!chr}))
+ echo "Input : str = ${!str}, char = ${!chr}"
+ echo "Output : (${sd[@]})"
+ echo ""
+done
+
diff --git a/challenge-248/nelo-tovar/perl/ch-1.pl b/challenge-248/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..4328fc285a
--- /dev/null
+++ b/challenge-248/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 248 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-248/
+#
+# Task 1 - Shortest Distance
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::MoreUtils qw(indexes minmax);
+use List::Util qw(min);
+use Data::Dump qw(dump);
+
+my @examples_str = (
+ "loveleetcode",
+ "aaab",
+);
+
+my @examples_char = (
+ 'e',
+ 'b',
+);
+
+sub shortest_distance {
+ my @str = split //, shift;
+ my $char = shift;
+ my @indexes = indexes { $_ eq $char } @str;
+ my @distances = ();
+
+ @distances = map {my $index = $_;min(map {abs($_ - $index)} @indexes)} 0 .. $#str;
+
+ return \@distances;
+}
+
+#for my $elements (@examples) {
+my $length = scalar @examples_str;
+for (my $i = 0; $i < $length; $i++) {
+
+ my $sd = shortest_distance $examples_str[$i], $examples_char[$i];
+
+ say "Input : str = \"$examples_str[$i]\", char = \"$examples_char[$i]\"";
+ say 'Output : ', dump(@$sd);
+ say ' ';
+}
diff --git a/challenge-248/nelo-tovar/perl/ch-2.pl b/challenge-248/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..6522c59419
--- /dev/null
+++ b/challenge-248/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 248 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-248/
+#
+# Task 2 - Submatrix Sum
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::Util qw (min max);
+use Algorithm::Combinatorics qw(combinations);
+use Data::Dump qw(dump);
+
+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]
+ ]
+);
+
+sub submatrix_sum {
+ my $matrix = shift;
+ my $rows = scalar @$matrix - 1 ;
+ my $cols = scalar @{$matrix->[0]} - 1 ;
+ my @sum = ();
+
+ for (my $i = 0; $i < $rows; $i++) {
+ for (my $k = 0; $k < $cols; $k++) {
+ $sum[$i][$k] = $matrix->[$i][$k] + $matrix->[$i][$k+1] +
+ $matrix->[$i+1][$k] + $matrix->[$i+1][$k+1];
+ }
+ }
+
+ return \@sum;
+}
+
+for my $elements (@examples) {
+ my $b = submatrix_sum $elements;
+
+ say 'Input : $a = [';
+ foreach my $x (@$elements) {
+ printf("%15s%s,\n", ' ', dump($x))
+ };
+ printf("%14s\n", ']');
+ say 'Output : $b = [ ';
+ foreach my $x (@$b) {
+ printf("%16s%s,\n", ' ', dump($x))
+ };
+ printf("%15s\n", ']');
+ say ' ';
+}