diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-01-17 20:14:17 -0800 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-01-17 20:14:17 -0800 |
| commit | 0601a6a3f12c64f5687246b5ef2707eff294acef (patch) | |
| tree | 5e09c6c33304e46ca939f0fc13088e3f4428e9d8 | |
| parent | ea10cb74a2ed5a0f5a5330378fff1a0aaa60237a (diff) | |
| download | perlweeklychallenge-club-0601a6a3f12c64f5687246b5ef2707eff294acef.tar.gz perlweeklychallenge-club-0601a6a3f12c64f5687246b5ef2707eff294acef.tar.bz2 perlweeklychallenge-club-0601a6a3f12c64f5687246b5ef2707eff294acef.zip | |
Robbie Hatley's perl solutions for The Weekly Challenge 252.
| -rw-r--r-- | challenge-252/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-252/robbie-hatley/perl/ch-1.pl | 123 | ||||
| -rwxr-xr-x | challenge-252/robbie-hatley/perl/ch-2.pl | 120 |
3 files changed, 244 insertions, 0 deletions
diff --git a/challenge-252/robbie-hatley/blog.txt b/challenge-252/robbie-hatley/blog.txt new file mode 100644 index 0000000000..06ba900413 --- /dev/null +++ b/challenge-252/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2024/01/robbie-hatleys-solutions-to-weekly_17.html
\ No newline at end of file diff --git a/challenge-252/robbie-hatley/perl/ch-1.pl b/challenge-252/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..b5e612d11e --- /dev/null +++ b/challenge-252/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,123 @@ +#!/usr/bin/env -S 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 252-1. +Written by Robbie Hatley on Wed Jan 17, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 252-1: Special Numbers +Submitted by: Mohammad S Anwar +You are given an array of integers, @ints. Write a script to find +the sum of the squares of all "special" elements of the given +array. An element $int[i] of @ints is called "special" if +i divides n, i.e. n % i == 0, where n is the length of the given +array. Also, the array is 1-indexed for the task. + +Example 1: +Input: @ints = (1, 2, 3, 4) +Output: 21 +There are exactly 3 special elements in the given array: +$ints[1] since 1 divides 4 +$ints[2] since 2 divides 4 +$ints[4] since 4 divides 4 +Hence, the sum of the squares of all special elements of the +given array is: +1 * 1 + 2 * 2 + 4 * 4 = 21. + +Example 2: +Input: @ints = (2, 7, 1, 19, 18, 3) +Output: 63 +There are exactly 4 special elements in the given array: +$ints[1] since 1 divides 6, +$ints[2] since 2 divides 6, +$ints[3] since 3 divides 6, and +$ints[6] since 6 divides 6. +Hence, the sum of the squares of all special elements of the +given array is: +2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63 + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +I'll handle this by making a sub with an accumulator $accum set to 0, then for each element of the array, +if it's "special", add its square to $a. To handle the "1 indexing", I'll let $i vary from 1 to $n, but use +[$i-1] for array indexing. + +-------------------------------------------------------------------------------------------------------------- +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 real numbers, in proper Perl syntax, like so: +./ch-1.pl '([-4, 17, -3, 42],[-0.875, -14.86, 9.307, 1.103, 4.43, 2.406])' + +Output is to STDOUT and will be each input array followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS AND MODULES USED: + +use v5.38; +use strict; +use warnings; +use utf8; +use warnings FATAL => 'utf8'; +use Sys::Binmode; +use Time::HiRes 'time'; + +# ------------------------------------------------------------------------------------------------------------ +# GLOBAL VARIABLES: +our $t0 ; # Seconds since 00:00:00 on Thu Jan 1, 1970. +our $db = 1; # Debug? Set to 0 for no, 1 for yes. + +# ------------------------------------------------------------------------------------------------------------ +# START TIMER: +BEGIN {$t0 = time} + +# ------------------------------------------------------------------------------------------------------------ +# SUBROUTINES: + +# Return sum of squares of special numbers: +sub sososn ($aref) { + my $n = scalar(@$aref); + my $accum = 0; + for ( my $i = 1 ; $i <= $n ; ++$i ) { + 0 == $n%$i and $accum += $$aref[$i-1]**2; + } + return $accum; +} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: + +# Inputs: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 Input: + [1, 2, 3, 4], + # Expected Output: 21 + + # Example 2 Input: + [2, 7, 1, 19, 18, 3], + # Expected Output: 63 +); + +# Main loop: +for my $aref (@arrays) { + say ''; + say 'Array = (' , join(', ', @$aref) , ')'; + say 'Sum of squares of special numbers = ', sososn($aref); +} +exit; + +# ------------------------------------------------------------------------------------------------------------ +# DETERMINE AND PRINT EXECUTION TIME: +END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)} +__END__ diff --git a/challenge-252/robbie-hatley/perl/ch-2.pl b/challenge-252/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..119ba7629e --- /dev/null +++ b/challenge-252/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,120 @@ +#!/usr/bin/env -S 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 252-2. +Written by Robbie Hatley on Wed Jan 17, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 252-2: Unique Sum Zero +Submitted by: Mohammad S Anwar +You are given an integer, $n. Write a script to find an array +containing $n unique integers such that they add up to zero. + +Example 1: +Input: $n = 5 +Output: (-7, -1, 1, 3, 4) +Two other valid solutions are: +(-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4). + +Example 2: +Input: $n = 3 +Output: (-1, 0, 1) + +Example 3: +Input: $n = 1 +Output: (0) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +This is actually much simpler than Task 1, for a change. The following algorithm provides one valid solution +(out of an always-infinite set of valid solutions) for each positive integer: Abort if $n is not a positive +integer. Make an empty array @a. Push (1..int($n/2)) to @a. If $n is odd, push 0 to @a. Then +push (-int($n/2)..-1) to @a. @a will now be $n elements long and will sum to 0, as the problem requires. + +-------------------------------------------------------------------------------------------------------------- +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 positive integers, in proper Perl syntax: +./ch-2.pl '("dog",1,3,7,18)' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS AND MODULES USED: + +use v5.38; +use strict; +use warnings; +use utf8; +use warnings FATAL => 'utf8'; +use Sys::Binmode; +use Time::HiRes 'time'; + +# ------------------------------------------------------------------------------------------------------------ +# GLOBAL VARIABLES: +our $t0 ; # Seconds since 00:00:00 on Thu Jan 1, 1970. +our $db = 1; # Debug? Set to 0 for no, 1 for yes. + +# ------------------------------------------------------------------------------------------------------------ +# START TIMER: +BEGIN {$t0 = time} + +# ------------------------------------------------------------------------------------------------------------ +# SUBROUTINES: + +sub is_pos_int ($x) { + return $x =~ m/^[1-9]\d*$/; +} + +sub usz ($n) { + return ('$n not pos int') if !is_pos_int($n); + my @a = (); + push @a, -int($n/2)..-1; + 1 == $n%2 and push @a, 0; + push @a, 1..int($n/2); + return @a; +} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: + +# Inputs: +my @inputs = @ARGV ? eval($ARGV[0]) : +( + # Example 1 Input: + 5, + # Expected Output: (-2, -1, 0, 1, 2) + + # Example 2 Input: + 3, + # Expected Output: (-1, 0, 1) + + # Example 3 Input: + 1, + # Expected Output: (0) +); + +# Main loop: +for my $n (@inputs) { + say ''; + say 'Input = ', $n; + !is_pos_int($n) and say 'Error: input not positive integer.' and say 'Skipping to next input' and next; + say 'Output = (' , join(', ', usz($n)) , ')'; +} +exit; + +# ------------------------------------------------------------------------------------------------------------ +# DETERMINE AND PRINT EXECUTION TIME: +END {my $µs = 1000000 * (time - $t0);printf("\nExecution time was %.0fµs.\n", $µs)} +__END__ |
