aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-07-11 09:32:51 +0100
committerGitHub <noreply@github.com>2023-07-11 09:32:51 +0100
commit0110f0dab8191790ed4e919d3f49f40a2f069f22 (patch)
tree2e04c3406b8e94827465f5ab60ad9313225895c9
parentd4de15ba21ed8ba090374b25e2a91e5c3972a798 (diff)
parent04ae72384683dc2f1a8b056b01719187c20e16a4 (diff)
downloadperlweeklychallenge-club-0110f0dab8191790ed4e919d3f49f40a2f069f22.tar.gz
perlweeklychallenge-club-0110f0dab8191790ed4e919d3f49f40a2f069f22.tar.bz2
perlweeklychallenge-club-0110f0dab8191790ed4e919d3f49f40a2f069f22.zip
Merge pull request #8348 from robbie-hatley/225
Robbie Hatley's solutions to The Weekly Challenge 225.
-rw-r--r--challenge-225/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-225/robbie-hatley/perl/ch-1.pl104
-rwxr-xr-xchallenge-225/robbie-hatley/perl/ch-2.pl156
3 files changed, 261 insertions, 0 deletions
diff --git a/challenge-225/robbie-hatley/blog.txt b/challenge-225/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..ff96bb5c35
--- /dev/null
+++ b/challenge-225/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2023/07/robbie-hatleys-solutions-to-weekly_9.html \ No newline at end of file
diff --git a/challenge-225/robbie-hatley/perl/ch-1.pl b/challenge-225/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..cbcca1f25a
--- /dev/null
+++ b/challenge-225/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,104 @@
+#! /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:
+ch-1.pl
+Solutions in Perl for The Weekly Challenge 225-1.
+Written by Robbie Hatley on Sunday July 9, 2023.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 1: Max Words
+Submitted by: Mohammad S Anwar
+Given a list of sentences, write a script to find the maximum word count of the sentences of the list.
+
+Example 1:
+Input:
+ @list =
+ (
+ "Perl and Raku belong to the same family.",
+ "I love Perl.",
+ "The Perl and Raku Conference."
+ );
+Output: 8
+
+Example 2:
+Input:
+ @list =
+ (
+ "The Weekly Challenge.",
+ "Python is the most popular guest language.",
+ "Team PWC has over 300 members.",
+ );
+Output: 7
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+This is just a matter of using split to parse the sentences of each list to words, then counting the words,
+then using List::Util 'max' to find the maximum word count of any sentence in the list. Something like this:
+my $max = max map {scalar split /[ .]+/, $_} @sentences;
+
+--------------------------------------------------------------------------------------------------------------
+IO NOTES:
+Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a
+double-quoted array of arrays of single-quoted sentences in proper Perl syntax, apostrophes escaped, like so:
+./ch-1.pl "(['I go.', 'She ran home.', 'I ate seven hot dogs.'],['She sat.', 'I didn\'t sit.'])"
+
+Output is to STDOUT and will be each list of sentences, followed by max word count.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRELIMINARIES:
+use v5.36;
+use strict;
+use warnings;
+use utf8;
+use Sys::Binmode;
+use Time::HiRes 'time';
+use List::Util 'max';
+$"=', ';
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+
+# Start timer:
+my $t0 = time;
+
+# Default inputs:
+my @lists =
+(
+ [
+ "Perl and Raku belong to the same family.",
+ "I love Perl.",
+ "The Perl and Raku Conference.",
+ ],
+ [
+ "The Weekly Challenge.",
+ "Python is the most popular guest language.",
+ "Team PWC has over 300 members.",
+ ],
+);
+
+# Non-default inputs:
+if (@ARGV) {@lists = eval($ARGV[0]);}
+
+# Main loop:
+for my $sentences (@lists) {
+ my $max = max map {scalar split /[ .]+/, $_} @$sentences;
+ say '';
+ say 'Sentences:';
+ say for @$sentences;
+ say "Max word count = $max";
+}
+
+# Determine and print execution time:
+my $µs = 1000000 * (time - $t0);
+printf("\nExecution time was %.0fµs.\n", $µs);
diff --git a/challenge-225/robbie-hatley/perl/ch-2.pl b/challenge-225/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..9c49ef8bab
--- /dev/null
+++ b/challenge-225/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,156 @@
+#! /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:
+ch-2.pl
+Solutions in Perl for The Weekly Challenge 225-2.
+Written by Robbie Hatley on Sunday July 9, 2023.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 2: Left Right Sum Diff
+Submitted by: Mohammad S Anwar
+You are given an array of integers, @ints.
+Write a script to return the left right sum diff array as shown below:
+@ints = (a, b, c, d, e)
+@left = ( 0, a, (a+b), (a+b+c), (a+b+c+d) )
+@right = ( (b+c+d+e), (c+d+e), (d+e), e, 0 )
+@left_right_sum_diff =
+(
+ | 0 - (b+c+d+e) |,
+ | a - (c+d+e) |,
+ | (a+b) - (d+e) |,
+ | (a+b+c) - e |,
+ | (a+b+c+d) - 0 |,
+)
+
+Example 1:
+Input: @ints = (10, 4, 8, 3)
+Output: (15, 1, 11, 22)
+@left = (0, 10, 14, 22)
+@right = (15, 11, 3, 0)
+@left_right_sum_diff = ( |0-15|, |10-11|, |14-3|, |22-0|)
+ = (15, 1, 11, 22)
+
+Example 2:
+Input: @ints = (1)
+Output: (0)
+@left = (0)
+@right = (0)
+@left_right_sum_diff = ( |0-0| ) = (0)
+
+Example 3:
+Input: @ints = (1, 2, 3, 4, 5)
+Output: (14, 11, 6, 1, 19)
+@left = (0, 1, 3, 6, 10)
+@right = (14, 12, 9, 5, 0)
+@left_right_sum_diff = ( |0-14|, |1-12|, |3-9|, |6-5|, |10-0|)
+ = (14, 11, 6, 1, 10)
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+This is just a matter of making subs to compute "left" and "right" sequences of partial sums of as defined
+by this problem (which is NOT the way math textbooks define "sequence of partial sums", by the way),
+then computing the array of absolute values of index-wise differences between @left and @right.
+
+--------------------------------------------------------------------------------------------------------------
+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 integers in proper Perl syntax, like so:
+./ch-2.pl '([13,0,96,-8,3],[11,2,6,4,-83,-42])'
+
+Output is to STDOUT and will be each input array, followed by the corresponding Left-Right Sum-Diff Array.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRELIMINARIES:
+use v5.36;
+use strict;
+use warnings;
+use utf8;
+use Sys::Binmode;
+use Time::HiRes 'time';
+$"=', ';
+
+# ------------------------------------------------------------------------------------------------------------
+# SUBROUTINES:
+sub lsps ($aref) { # "lsps" = "Left Sequence of Partial Sums"
+ my $size = scalar @$aref;
+ my @lsps = ();
+ for ( my $i = 0 ; $i < $size ; ++$i ) {
+ my $term = 0;
+ for ( my $j = 0 ; $j < $i ; ++$j ) {
+ $term += $$aref[$j];
+ }
+ push @lsps, $term;
+ }
+ return @lsps
+}
+
+sub rsps ($aref) { # "rsps" = "Right Sequence of Partial Sums"
+ my $size = scalar @$aref;
+ my @lsps = ();
+ for ( my $i = 0 ; $i < $size ; ++$i ) {
+ my $term = 0;
+ for ( my $j = $i+1 ; $j < $size ; ++$j ) {
+ $term += $$aref[$j];
+ }
+ push @lsps, $term;
+ }
+ return @lsps
+}
+
+sub diff ($aref1, $aref2) { # "diff" = "difference of sequences"
+ my $size1 = scalar @$aref1;
+ my $size2 = scalar @$aref2;
+ if ($size1 != $size2) {
+ die "Error in sub \"dos\": arrays not same size!\n$!\n";
+ }
+ my @diffs;
+ for ( my $i = 0 ; $i < $size1 ; ++$i ) {
+ push @diffs, abs($$aref1[$i]-$$aref2[$i]);
+ }
+ return @diffs;
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+
+# Start timer:
+my $t0 = time;
+
+# Default inputs:
+my @arrays =
+(
+ [10, 4, 8, 3],
+ [1],
+ [1, 2, 3, 4, 5],
+);
+
+# Non-default inputs:
+if (@ARGV) {@arrays = eval($ARGV[0]);}
+
+# Main loop:
+for my $aref (@arrays) {
+ my @lsps = lsps($aref);
+ my @rsps = rsps($aref);
+ my @diff = diff(\@lsps, \@rsps);
+ say '';
+ say "array = (@$aref)";
+ say "lsps = (@lsps)";
+ say "rsps = (@rsps)";
+ say "diff = (@diff)";
+}
+
+# Determine and print execution time:
+my $µs = 1000000 * (time - $t0);
+printf("\nExecution time was %.0fµs.\n", $µs);
+exit 0;