aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-256/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-256/robbie-hatley/perl/ch-1.pl116
-rwxr-xr-xchallenge-256/robbie-hatley/perl/ch-2.pl87
3 files changed, 204 insertions, 0 deletions
diff --git a/challenge-256/robbie-hatley/blog.txt b/challenge-256/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..906f8ad6f6
--- /dev/null
+++ b/challenge-256/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2024/02/robbie-hatleys-solutions-to-weekly_13.html \ No newline at end of file
diff --git a/challenge-256/robbie-hatley/perl/ch-1.pl b/challenge-256/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..aaefb38f49
--- /dev/null
+++ b/challenge-256/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,116 @@
+#!/usr/bin/perl -CSDA
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+COLOPHON:
+This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A").
+¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。
+
+--------------------------------------------------------------------------------------------------------------
+TITLE BLOCK:
+Solutions in Perl for The Weekly Challenge 256-1.
+Written by Robbie Hatley on Mon Feb 12, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 256-1: Maximum Pairs
+Submitted by: Mohammad Sajid Anwar
+You are given an array of distinct words, @words. Write a script
+to find the maximum pairs in the given array. The words $words[i]
+and $words[j] can be a "pair" if one is reverse of the other.
+
+Example 1:
+Input: @words = ("ab", "de", "ed", "bc")
+Output: 1
+There is one pair in the given array: "de" and "ed"
+
+Example 2:
+Input: @words = ("aa", "ba", "cd", "ed")
+Output: 0
+
+Example 3:
+Input: @words = ("uv", "qp", "st", "vu", "mn", "pq")
+Output: 2
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+This problem is easily solved by nested 3-part loops on index variables i and j such that j > i:
+
+# Reverse a string:
+sub reverse_string ($x) {
+ return join '', reverse split //, $x
+}
+
+# How many fwd/rev pairs are in @$words?
+sub pairs ($words) {
+ my $pairs = 0;
+ for ( my $i = 0 ; $i <= $#$words - 1 ; ++$i ) {
+ for ( my $j = $i + 1 ; $j <= $#$words - 0 ; ++$j ) {
+ if ( $$words[$i] eq reverse_string($$words[$j]) ) {
+ ++$pairs;
+ }
+ }
+ }
+ return $pairs;
+}
+
+--------------------------------------------------------------------------------------------------------------
+IO NOTES:
+Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
+single-quoted array of arrays of double-quoted words, in proper Perl syntax, like so:
+./ch-1.pl '(["fish","beef","pork"],["yak", "rat", "tar", "kay"])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS AND MODULES:
+use v5.38;
+
+# ------------------------------------------------------------------------------------------------------------
+# SUBROUTINES:
+
+# Reverse a string:
+sub reverse_string ($x) {
+ return join '', reverse split //, $x
+}
+
+# How many fwd/rev pairs are in @$words?
+sub pairs ($words) {
+ my $pairs = 0;
+ for ( my $i = 0 ; $i <= $#$words - 1 ; ++$i ) {
+ for ( my $j = $i + 1 ; $j <= $#$words ; ++$j ) {
+ if ( $$words[$i] eq reverse_string($$words[$j]) ) {
+ ++$pairs;
+ }
+ }
+ }
+ return $pairs;
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ ["ab", "de", "ed", "bc"],
+ # Expected Output: 1
+
+ # Example 2 Input:
+ ["aa", "ba", "cd", "ed"],
+ # Expected Output: 0
+
+ # Example 3 Input:
+ ["uv", "qp", "st", "vu", "mn", "pq"],
+ # Expected Output: 2
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN LOOP:
+for my $aref (@arrays) {
+ say '';
+ say 'Words: (', join(', ', map {"\"$_\""} @$aref), ')';
+ say 'Number of fwd/rev pairs = ', pairs($aref);
+}
diff --git a/challenge-256/robbie-hatley/perl/ch-2.pl b/challenge-256/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..dbc3dea625
--- /dev/null
+++ b/challenge-256/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,87 @@
+#!/usr/bin/perl -CSDA
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+COLOPHON:
+This is a 110-character-wide Unicode UTF-8 Perl-source-code text file with hard Unix line breaks ("\x0A").
+¡Hablo Español! Говорю Русский. Björt skjöldur. ॐ नमो भगवते वासुदेवाय. 看的星星,知道你是爱。麦藁雪、富士川町、山梨県。
+
+--------------------------------------------------------------------------------------------------------------
+TITLE BLOCK:
+Solutions in Perl for The Weekly Challenge 256-2.
+Written by Robbie Hatley on Mon Feb 12, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 256-2: Merge Strings
+Submitted by: Mohammad Sajid Anwar
+You are given two strings, $str1 and $str2. Write a script to
+merge the given strings by adding in alternative order
+starting with the first string. If a string is longer than the
+other then append the remaining at the end.
+
+Example 1:
+Input: $str1 = "abcd", $str2 = "1234"
+Output: "a1b2c3d4"
+
+Example 2:
+Input: $str1 = "abc", $str2 = "12345"
+Output: "a1b2c345"
+
+Example 3:
+Input: $str1 = "abcde", $str2 = "123"
+Output: "a1b2c3de"
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I could roll my own on this, but why? This problem is already solved by the "mesh" function from
+CPAN module "List::Util", so I'll just use that. Doing otherwise would be like engineering and
+manufacturing my own electric car at a cost of 5G$ when I could have bought a Tesla for 50k$.
+
+--------------------------------------------------------------------------------------------------------------
+IO NOTES:
+Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
+single-quoted array of arrays of two double-quoted strings, in proper Perl syntax, like this:
+./ch-2.pl '(["confabulated", "retroactive"],["AQGBT", "1379524"])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS AND MODULES USED:
+use v5.38;
+
+# ------------------------------------------------------------------------------------------------------------
+# SUBROUTINES:
+use List::Util 'mesh';
+no warnings 'uninitialized';
+sub mesh_strings ($x, $y) {
+ return join('', mesh([split //, $x], [split //, $y]));
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @pairs = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 Input:
+ ["abcd", "1234"],
+ # Expected Output: "a1b2c3d4"
+
+ # Example 2 Input:
+ ["abc", "12345"],
+ # Expected Output: "a1b2c345"
+
+ # Example 3 Input:
+ ["abcde", "123"],
+ # Expected Output: "a1b2c3de"
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN LOOP:
+for my $pair_ref (@pairs) {
+ say '';
+ say 'Strings: ', join(', ', map {"\"$_\""} @$pair_ref);
+ say 'Merged: ', join(', ', map {"\"$_\""} mesh_strings(@$pair_ref));
+}