From edd8d867978d3a7afefe3c263ee82dba7297e6ed Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Tue, 1 Aug 2023 13:47:44 -0700 Subject: 228-1 --- challenge-228/robbie-hatley/perl/ch-1.pl | 96 ++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 challenge-228/robbie-hatley/perl/ch-1.pl 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..2f2eb8bd23 --- /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])" + +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{$_}} # Record how many copies of each element exist. + my $sum = 0; # Variable to hold sum of unique elements + map { $sum += $_ if 1 == $ab{$_} } keys %ab; # Sum of 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 ''; + my $sum = sum_unique($aref); + say "Array = (@$aref)"; + say "Sum of unique elements = $sum"; +} + +# Determine and print execution time: +my $µs = 1000000 * (time - $t0); +printf("\nExecution time was %.0fµs.\n", $µs); -- cgit From c6e32a34ed432e6180f90596afb6e35add80f3c0 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Wed, 2 Aug 2023 01:40:52 -0700 Subject: Robbie Hatley's solutions to The Weekly Challenge 228 --- challenge-228/robbie-hatley/blog.txt | 1 + challenge-228/robbie-hatley/perl/ch-1.pl | 20 +++--- challenge-228/robbie-hatley/perl/ch-2.pl | 102 +++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 challenge-228/robbie-hatley/blog.txt create mode 100755 challenge-228/robbie-hatley/perl/ch-2.pl 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 index 2f2eb8bd23..d0c9a41906 100755 --- a/challenge-228/robbie-hatley/perl/ch-1.pl +++ b/challenge-228/robbie-hatley/perl/ch-1.pl @@ -38,7 +38,7 @@ 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])" +./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. @@ -51,19 +51,21 @@ 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{$_}} # Record how many copies of each element exist. - my $sum = 0; # Variable to hold sum of unique elements - map { $sum += $_ if 1 == $ab{$_} } keys %ab; # Sum of 1-of-a-kind elements. - return $sum; # Return sum. + 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. } # ------------------------------------------------------------------------------------------------------------ @@ -85,10 +87,8 @@ my @arrays = # Main loop: for my $aref (@arrays) { - say ''; - my $sum = sum_unique($aref); - say "Array = (@$aref)"; - say "Sum of unique elements = $sum"; + say "\nArray = (@$aref)"; + say "Sum of unique elements = ", sum_unique($aref); } # Determine and print execution time: 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); -- cgit