aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-08-02 13:41:37 +0100
committerGitHub <noreply@github.com>2023-08-02 13:41:37 +0100
commitb658dc62f92bb59e0d9f55a7fcc87ff2876abac0 (patch)
tree8913fde3581bc2197e463cc27866c415f94ebac7
parente7f919d06cc8f95c1364ea719bf5c9d634bcaebf (diff)
parentc6e32a34ed432e6180f90596afb6e35add80f3c0 (diff)
downloadperlweeklychallenge-club-b658dc62f92bb59e0d9f55a7fcc87ff2876abac0.tar.gz
perlweeklychallenge-club-b658dc62f92bb59e0d9f55a7fcc87ff2876abac0.tar.bz2
perlweeklychallenge-club-b658dc62f92bb59e0d9f55a7fcc87ff2876abac0.zip
Merge pull request #8494 from robbie-hatley/228
Robbie Hatley's solutions to The Weekly Challenge 228
-rw-r--r--challenge-228/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-228/robbie-hatley/perl/ch-1.pl96
-rwxr-xr-xchallenge-228/robbie-hatley/perl/ch-2.pl102
3 files changed, 199 insertions, 0 deletions
diff --git a/challenge-228/robbie-hatley/blog.txt b/challenge-228/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..6ad4fbbe16
--- /dev/null
+++ b/challenge-228/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2023/08/robbie-hatleys-solutions-to-weekly.html \ No newline at end of file
diff --git a/challenge-228/robbie-hatley/perl/ch-1.pl b/challenge-228/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..d0c9a41906
--- /dev/null
+++ b/challenge-228/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,96 @@
+#! /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 228-1.
+Written by Robbie Hatley on Tue Aug 01, 2023.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 1: Unique Sum
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+Write a script to find out the sum of unique elements in the given array.
+
+Example 1: Input: @int = (2, 1, 3, 2) Output: 4
+In the given array we have 2 unique elements (1, 3).
+
+Example 2: Input: @int = (1, 1, 1, 1) Output: 0
+In the given array no unique element found.
+
+Example 3: Input: @int = (2, 1, 3, 4) Output: 10
+In the given array every element is unique.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+To solve this problem, I'll make a hash of the number of instances of each element in an array, then map a sum
+of just those hash keys with values of exactly 1.
+
+--------------------------------------------------------------------------------------------------------------
+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 integers, in proper Perl syntax, like so:
+./ch-1.pl "([1,-4,2,-2,-4,-5,7,-3],[17,3,76,3,7,3],[8,73,11,8,73,11])"
+
+Output is to STDOUT and will be each input array followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRELIMINARIES:
+
+use v5.36;
+use strict;
+use warnings;
+use utf8;
+
+use Sys::Binmode;
+use Time::HiRes 'time';
+
+$"=', ';
+
+# ------------------------------------------------------------------------------------------------------------
+# SUBROUTINES:
+
+sub sum_unique ($aref) {
+ my %ab; # Hash to hold element abundances.
+ for (@$aref) {++$ab{$_}} # Autovivify & increment from undef to 1.
+ my $sum = 0; # Variable to hold sum.
+ map {$sum += $_ if 1 == $ab{$_}} keys %ab; # Sum 1-of-a-kind elements.
+ return $sum; # Return sum.
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+
+# Start timer:
+my $t0 = time;
+
+# Default inputs:
+my @arrays =
+(
+ [2, 1, 3, 2],
+ [1, 1, 1, 1],
+ [2, 1, 3, 4],
+);
+
+# Non-default inputs:
+@arrays = eval($ARGV[0]) if @ARGV;
+
+# Main loop:
+for my $aref (@arrays) {
+ say "\nArray = (@$aref)";
+ say "Sum of unique elements = ", sum_unique($aref);
+}
+
+# Determine and print execution time:
+my $µs = 1000000 * (time - $t0);
+printf("\nExecution time was %.0fµs.\n", $µs);
diff --git a/challenge-228/robbie-hatley/perl/ch-2.pl b/challenge-228/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..2b747077db
--- /dev/null
+++ b/challenge-228/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,102 @@
+#! /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 228-2.
+Written by Robbie Hatley on Wed Aug 02, 2023.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 2: Empty Array
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers in which all elements are unique. Write a script to perform the following
+operations until the array is empty, and return the total count of operations: If the first element is the
+smallest then remove it otherwise move it to the end.
+
+Example 1: Input: @int = (3, 4, 2) Output: 5
+Operation 1: move 3 to the end: (4, 2, 3)
+Operation 2: move 4 to the end: (2, 3, 4)
+Operation 3: remove element 2: (3, 4)
+Operation 4: remove element 3: (4)
+Operation 5: remove element 4: ()
+
+Example 2: Input: @int = (1, 2, 3) Output: 3
+Operation 1: remove element 1: (2, 3)
+Operation 2: remove element 2: (3)
+Operation 3: remove element 3: ()
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I use List::Util::min, shift, and push, and increment a counter:
+
+--------------------------------------------------------------------------------------------------------------
+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 uniq integers, in proper Perl syntax, like so:
+./ch-2.pl "([9,2,'dog',7],[3,7,3,7],[1,6,2,7,3,8,4,9],[8,7,6,5,4,3,2,1])"
+
+Output is to STDOUT and will be each input array followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRELIMINARIES:
+
+use v5.36;
+use strict;
+use warnings;
+use utf8;
+
+use Sys::Binmode;
+use Time::HiRes 'time';
+use List::Util 'min';
+
+$"=', ';
+
+# ------------------------------------------------------------------------------------------------------------
+# SUBROUTINES:
+
+sub num_moves ($aref) {
+ my @array = @$aref; # Grab copy of original array.
+ my $moves = 0; # Count moves.
+ while (@array) { # For each element,
+ $array[0] == min @array and shift @array # if first is min, discard first;
+ or push @array, shift @array; # otherwise, move first to last.
+ ++$moves; # Increment moves.
+ }
+ return $moves; # Return total moves.
+}
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+
+# Start timer:
+my $t0 = time;
+
+# Default inputs:
+my @arrays =
+(
+ [3, 4, 2],
+ [1, 2, 3],
+);
+
+# Non-default inputs:
+@arrays = eval($ARGV[0]) if @ARGV;
+
+# Main loop:
+for my $aref (@arrays) {
+ say "\nArray = (@$aref)";
+ say "Number of moves = ", num_moves $aref;
+}
+
+# Determine and print execution time:
+my $µs = 1000000 * (time - $t0);
+printf("\nExecution time was %.0fµs.\n", $µs);