diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-06-04 22:20:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-04 22:20:23 +0100 |
| commit | 4fe714e71d2b16c0761cfccc5a3fee070a8e78ea (patch) | |
| tree | cb161ca7bafc1838e06ece912a83a9ee507cf8f8 | |
| parent | 49fd1c574eff95574cc8a410f7764eeed8edd0e3 (diff) | |
| parent | 5e4cc9a49b6dac8e6abb2004c2b04a6102642644 (diff) | |
| download | perlweeklychallenge-club-4fe714e71d2b16c0761cfccc5a3fee070a8e78ea.tar.gz perlweeklychallenge-club-4fe714e71d2b16c0761cfccc5a3fee070a8e78ea.tar.bz2 perlweeklychallenge-club-4fe714e71d2b16c0761cfccc5a3fee070a8e78ea.zip | |
Merge pull request #8177 from robbie-hatley/219
Robbie Hatley's Perl solution to The Weekly Challenge 219-1.
| -rw-r--r-- | challenge-219/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-219/robbie-hatley/perl/ch-1.pl | 72 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-219/robbie-hatley/blog.txt b/challenge-219/robbie-hatley/blog.txt new file mode 100644 index 0000000000..51a803baf4 --- /dev/null +++ b/challenge-219/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/06/robbie-hatleys-solutions-to-weekly.html
\ No newline at end of file diff --git a/challenge-219/robbie-hatley/perl/ch-1.pl b/challenge-219/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..9034937bcb --- /dev/null +++ b/challenge-219/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,72 @@ +#! /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: +ch-1.pl +Solutions in Perl for The Weekly Challenge 219-1. +Written by Robbie Hatley on Fri Jun 02, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 1: Sorted Squares +Submitted by: Mohammad S Anwar +Given a list of numbers, write a script to square each number in the list and return the sorted list of +squares in increasing order. +Example 1: Input: (-2, -1, 0, 3, 4) Output: (0, 1, 4, 9, 16) +Example 2: Input: (5, -4, -1, 3, 6) Output: (1, 9, 16, 25, 36) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +Easy; just do 'for my $aref (@arrays) {my squares = sort {$a<=>$b} map {$_*$_} @$aref; say "(@squares)";}' + +-------------------------------------------------------------------------------------------------------------- +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 in proper Perl syntax, like so: +./ch-1.pl '([13.2,0,-96.3],[-8.1,3.7,11.92],[2,-6.03,4.371])' + +Output is to STDOUT and will be each input array followed by the corresponding sorted list of squares. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRELIMINARIES: +use v5.36; +use strict; +use warnings; +use utf8; +use Sys::Binmode; +use Time::HiRes 'time'; +$"=', '; + +# ------------------------------------------------------------------------------------------------------------ +# DEFAULT INPUTS: +my @arrays = +( + [-2, -1, 0, 3, 4], + [5, -4, -1, 3, 6], +); + +# ------------------------------------------------------------------------------------------------------------ +# NON-DEFAULT INPUTS: +if (@ARGV) {@arrays = eval($ARGV[0]);} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +my $t0 = time; +for my $aref (@arrays) { + my @squares = sort {$a<=>$b} map {$_*$_} @$aref; + say ''; + say "array = (@$aref)"; + say "squares = (@squares)"; +} +my $µs = 1000000 * (time - $t0); +printf("\nExecution time was %.3fµs.\n", $µs); +exit 0; |
