aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-226/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-226/robbie-hatley/perl/ch-1.pl121
-rwxr-xr-xchallenge-226/robbie-hatley/perl/ch-2.pl114
3 files changed, 236 insertions, 0 deletions
diff --git a/challenge-226/robbie-hatley/blog.txt b/challenge-226/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..3ebc6a5935
--- /dev/null
+++ b/challenge-226/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2023/07/robbie-hatleys-solutions-to-weekly_18.html \ No newline at end of file
diff --git a/challenge-226/robbie-hatley/perl/ch-1.pl b/challenge-226/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..1e087cc1ed
--- /dev/null
+++ b/challenge-226/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,121 @@
+#! /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 226-1.
+Written by Robbie Hatley on Mon Jul 17, 2023.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 1: Shuffle String
+Submitted by: Mohammad S Anwar
+You are given a string and an array of indices of same length as string.
+Write a script to return the string after re-arranging the indices in the correct order.
+
+Example 1:
+Input: $string = 'lacelengh', @indices = (3,2,0,5,4,8,6,7,1)
+Output: 'challenge'
+
+Example 2:
+Input: $string = 'rulepark', @indices = (4,7,3,1,0,5,2,6)
+Output: 'perlraku'
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+This problem is the exact opposite of a Perl "slice", in that it asks not "What would this string look like
+if rearranged in this order?", but rather, "What original string would look like THIS if rearranged in THIS
+order?", which is a different question altogether. Fine, I'll start with a string of the same length but
+consisting only of Unicode "invalid character" characters, "�". Then I'll over-write the given indices of
+that string with the same-position characters of the given "scrambled" string.
+
+--------------------------------------------------------------------------------------------------------------
+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 in proper Perl syntax. Each inner array must consist of a string followed by a
+sequence of indices, which must be integers in the range 0..n-1 where n is the length of the string, like so:
+./ch-1.pl "(['a si saftsic SeDsitna',12,11,10,9,8,16,15,14,20,19,18,17,13,2,1,0,7,6,5,4,3])"
+
+Output is to STDOUT and will be each input array followed by corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRELIMINARIES:
+
+use v5.36;
+use strict;
+use warnings;
+use utf8;
+use Sys::Binmode;
+use Time::HiRes 'time';
+$"=', ';
+
+# ------------------------------------------------------------------------------------------------------------
+# SUBROUTINES:
+
+sub indices_are_valid ($string, @indices) {
+ my $max_index = length($string)-1;
+ for (@indices) {
+ return 0 if $_ < 0 || $_ > $max_index;
+ }
+ return 1;
+}
+
+sub indices_are_bijective ($string, @indices) {
+ return 0 if scalar(@indices) != length($string);
+ my @sorted = sort {$a<=>$b} map {0+$_} @indices;
+ for ( my $i = 0 ; $i <= $#sorted ; ++$i ) {
+ return 0 if $sorted[$i] != $i;
+ }
+ return 1;
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+
+# Start timer:
+my $t0 = time;
+
+# Default inputs:
+my @arrays =
+(
+ ['lacelengh', 3,2,0,5,4,8,6,7,1],
+ ['rulepark', 4,7,3,1,0,5,2,6],
+);
+
+# Non-default inputs:
+@arrays = eval($ARGV[0]) if @ARGV;
+
+# Main loop:
+for my $aref (@arrays) {
+ say '';
+ my @indices = @$aref;
+ my $shuffled = shift @indices;
+ say "Shuffled: \"$shuffled\"";
+ say "Indices: (@indices)";
+ my $length = length($shuffled);
+ if ( ! indices_are_valid($shuffled,@indices) ) {
+ say "Error: given indices are not valid for a string of length $length";
+ say "(out-of-range values are present). Skipping to next string and indices.";
+ next;
+ }
+ if ( ! indices_are_bijective($shuffled,@indices) ) {
+ say "Error: given indices are not a remapping of a string of length $length";
+ say "(gaps and/or overlaps are present). Skipping to next string and indices.";
+ next;
+ }
+ my $original = '�' x length($shuffled);
+ substr($original, $indices[$_], 1, substr($shuffled,$_,1)) for 0..$#indices;
+ say "Original: \"$original\"";
+}
+
+# Determine and print execution time:
+my $µs = 1000000 * (time - $t0);
+printf("\nExecution time was %.0fµs.\n", $µs);
diff --git a/challenge-226/robbie-hatley/perl/ch-2.pl b/challenge-226/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..f441edc716
--- /dev/null
+++ b/challenge-226/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,114 @@
+#! /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 226-2.
+Written by Robbie Hatley on Mon Jul 17, 2023.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 2: Zero Array
+Submitted by: Mohammad S Anwar
+
+You are given an array of non-negative integers, @ints. Write a script to return the minimum number of
+operations to make every element equal zero. In each operation, you are required to pick a positive number
+less than or equal to the smallest element in the array, then subtract that from each positive element in
+the array.
+
+Example 1:
+Input: @ints = (1, 5, 0, 3, 5)
+Output: 3
+operation 1: pick 1 => (0, 4, 0, 2, 4)
+operation 2: pick 2 => (0, 2, 0, 0, 2)
+operation 3: pick 2 => (0, 0, 0, 0, 0)
+
+Example 2:
+Input: @ints = (0)
+Output: 0
+
+Example 3:
+Input: @ints = (2, 1, 4, 0, 3)
+Output: 4
+operation 1: pick 1 => (1, 0, 3, 0, 2)
+operation 2: pick 1 => (0, 0, 2, 0, 1)
+operation 3: pick 1 => (0, 0, 1, 0, 0)
+operation 4: pick 1 => (0, 0, 0, 0, 0)
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+There is no need to actually do any "operations". All we need to note is that the number of operations
+required will be equal to the number of unique positive integers present. So I'll use List::Util,
+do a "uniq sort", and shift the first number if it's 0.
+
+--------------------------------------------------------------------------------------------------------------
+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 non-negative integers, in proper Perl syntax, like so:
+./ch-2.pl "([0,17,6,17,4,2],[1,2,3,4,5,6])"
+
+Output is to STDOUT and will be each input array followed by corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRELIMINARIES:
+
+use v5.36;
+use strict;
+use warnings;
+use utf8;
+use Sys::Binmode;
+use Time::HiRes 'time';
+use List::Util 'uniq';
+$"=', ';
+
+# ------------------------------------------------------------------------------------------------------------
+# SUBROUTINES:
+sub are_non_neg_ints ($aref) {
+ for (@$aref) {
+ return 0 if $_ !~ m/(?:^0$)|(?:^[1-9]\d*$)/;
+ }
+ return 1;
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+
+# Start timer:
+my $t0 = time;
+
+# Default inputs:
+my @arrays =
+(
+ [1, 5, 0, 3, 5],
+ [0],
+ [2, 1, 4, 0, 3],
+);
+
+# Non-default inputs:
+@arrays = eval($ARGV[0]) if @ARGV;
+
+# Main loop:
+for my $aref (@arrays) {
+ say '';
+ say "Array: (@$aref)";
+ if ( ! are_non_neg_ints($aref) ) {
+ say "Error: array must contain only non-negative integers.";
+ say "Moving on to next array.";
+ next;
+ }
+ my @nums = uniq sort {$a<=>$b} @$aref;
+ shift @nums if 0 == $nums[0];
+ say "Number of operations required = ", scalar(@nums);
+}
+
+# Determine and print execution time:
+my $µs = 1000000 * (time - $t0);
+printf("\nExecution time was %.0fµs.\n", $µs);