aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-197/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-197/robbie-hatley/perl/ch-1.pl55
-rwxr-xr-xchallenge-197/robbie-hatley/perl/ch-2.pl73
3 files changed, 129 insertions, 0 deletions
diff --git a/challenge-197/robbie-hatley/blog.txt b/challenge-197/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..e0c5f13b00
--- /dev/null
+++ b/challenge-197/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2022/12/robbie-hatleys-solutions-to-perl-weekly.html \ No newline at end of file
diff --git a/challenge-197/robbie-hatley/perl/ch-1.pl b/challenge-197/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..bb165912c8
--- /dev/null
+++ b/challenge-197/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#! /usr/bin/perl
+
+# Robbie Hatley's Perl solution to challenge 197-1
+
+=pod
+
+Task 1: Move Zero
+Submitted by: Mohammad S Anwar
+You are given a list of integers, @list. Write a script to move all zeros,
+if any exist, to the end of the list, while maintaining the relative order
+of non-zero elements.
+
+Example 1: Input: (1, 0, 3, 0, 0, 5) Output: (1, 3, 5, 0, 0, 0)
+Example 2: Input: (1, 6, 4) Output: (1, 6, 4)
+Example 3: Input: (0, 1, 0, 2, 0) Output: (1, 2, 0, 0, 0)
+
+=cut
+
+# NOTE: Input is from built-in array or command-line args.
+# If using args, they should be space-separated integers.
+
+# NOTE: Output is to stdout and will be a printout of each input array,
+# followed by the same with all 0s moved to end.
+
+# Preliminaries:
+use v5.36;
+use List::Util 'sum0';
+
+# Default Inputs:
+my @lists =
+ (
+ [1, 0, 3, 0, 0, 5],
+ [1, 6, 4],
+ [0, 1, 0, 2, 0]
+ );
+
+# Non-Default Input:
+if (@ARGV) {@lists=([@ARGV])}
+
+# Move zero! Move zero! For great justice, take off every zero!
+# ~~AYBABTU
+
+for (@lists){
+ my @list = @{$_}; # Input.
+ my $z; # A placeholder for a 0.
+ say ''; # Print blank line.
+ say "Input = (@list)"; # Announce input.
+ for ( my $i = 0 ; $i <= $#list ; ++$i ){ # Riffle through list.
+ last if 0 == sum0(@list[$i..$#list]); # Stop if all remaining elements are 0.
+ if ( 0 == $list[$i] ){ # If we find a 0,
+ $z = splice @list, $i, 1; # splice it from current position
+ splice @list, @list, 0, $z; # and paste it on end.
+ --$i;}} # Back-up one spot,
+ # because we altered current location.
+ say "Output = (@list)";} # Print output. \ No newline at end of file
diff --git a/challenge-197/robbie-hatley/perl/ch-2.pl b/challenge-197/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..7a048efef4
--- /dev/null
+++ b/challenge-197/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#! /usr/bin/perl
+
+# Robbie Hatley's Perl solution to challenge 197-2
+
+=pod
+
+Task 2:
+Submitted by: Mohammad S Anwar
+You are given a list of integers, @list.
+Write a script to perform a Wiggle Sort on the given list.
+(A "Wiggle Sort" would be such as list[0] < list[1] > list[2] < list[3]....)
+
+Example 1: Input: (1,5,1,1,6,4) Output: (1,6,1,5,1,4)
+Example 2: Input: (1,3,2,2,3,1) Output: (2,3,1,3,1,2)
+
+WARNING: This is not the usual way of defining "Wiggle Sort", which
+ normally uses "<=" and ">=" instead of "<" and ">".
+ Let's call this version "Strong Wiggle Sort".
+
+WARNING: Not all inputs are CAPABLE of being Strong-Wiggle-Sorted.
+
+WARNING: Both Weak and Strong Wiggle Sort are non-deterministic,
+ so there are often multiple valid wiggle-sortings of an array.
+
+=cut
+
+# NOTE: Input is from built-in array or command-line args.
+# If using args, they should be space-separated integers.
+
+# NOTE: Output is to stdout and will be a printout of each input array,
+# followed by either a strong-wiggle-sorted version of the input,
+# or by "timed out" if no valid sorting could be found in 1000 attempts.
+
+# Preliminaries:
+use v5.36;
+
+# Default Inputs:
+my @arrays=
+(
+ [1,5,1,1,6,4],
+ [1,3,2,2,3,1]
+);
+
+# Non-Default Input:
+if (@ARGV) {@arrays=([@ARGV])}
+
+# Main body of script:
+ARRAY : for (@arrays){ # For each input array.
+ my @array = @{$_}; # Current input array.
+ say ''; # Print blank line.
+ say "Input = (@array)"; # Print input.
+ my $flag = 1; # We have work to do.
+ my $shifts = 0; # How many shifts have we worked?
+ while ( $flag ){ # Loop while we have work to do.
+ if ( $shifts >=1000 ){ # But if we've worked 1000 shifts,
+ say "timed out"; # and yet the work still ain't done,
+ next ARRAY;} # then give up.
+ ++$shifts; # Yet another work shift.
+ $flag = 0; # Work is done, maybe?
+ for ( my $i = 0 ; $i <= $#array - 1 ; ++$i ){ # For each index pair:
+ if ( $i % 2 ){ # If (odd,even) pair:
+ if ( $array[$i] <= $array[$i+1] ){ # If pair isn't downward-trending,
+ $flag = 1; # work's not done.
+ my $temp = $array[$i]; # swap
+ $array[$i] = $array[$i+1]; # swap
+ $array[$i+1] = $temp;}} # swap
+ else { # Else if (even,odd) pair:
+ if ( $array[$i] >= $array[$i+1] ){ # If pair isn't upward-trending,
+ $flag = 1; # work's not done.
+ my $temp = $array[$i]; # swap
+ $array[$i] = $array[$i+1]; # swap
+ $array[$i+1] = $temp;}}}} # swap
+ say "Output = (@array)";} # Print output. \ No newline at end of file