diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-18 14:51:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-18 14:51:17 +0100 |
| commit | e0feda4f140d4461cda645ff0978005f3f07a198 (patch) | |
| tree | 22234e173f857fe3af89dbfccdc3a353c79d1548 | |
| parent | 396595e581e1aeb82f71eb8df0769c7523521d07 (diff) | |
| parent | 45072ba632e6fc39500664a37da4ed1deff4a3ea (diff) | |
| download | perlweeklychallenge-club-e0feda4f140d4461cda645ff0978005f3f07a198.tar.gz perlweeklychallenge-club-e0feda4f140d4461cda645ff0978005f3f07a198.tar.bz2 perlweeklychallenge-club-e0feda4f140d4461cda645ff0978005f3f07a198.zip | |
Merge pull request #8099 from robbie-hatley/217
Robbie Hatley's solutions in Perl for The Weekly Challenge 217
| -rw-r--r-- | challenge-217/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-217/robbie-hatley/perl/ch-1.pl | 117 | ||||
| -rwxr-xr-x | challenge-217/robbie-hatley/perl/ch-2.pl | 115 |
3 files changed, 233 insertions, 0 deletions
diff --git a/challenge-217/robbie-hatley/blog.txt b/challenge-217/robbie-hatley/blog.txt new file mode 100644 index 0000000000..32b2aa41bc --- /dev/null +++ b/challenge-217/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/05/robbie-hatleys-solutions-to-weekly_18.html
\ No newline at end of file diff --git a/challenge-217/robbie-hatley/perl/ch-1.pl b/challenge-217/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..6777cac2ba --- /dev/null +++ b/challenge-217/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,117 @@ +#! /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 217-1. +Written by Robbie Hatley on Wed May 17, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 1: Sorted Matrix +Submitted by: Mohammad S Anwar +You are given a n x n matrix where n >= 2. +Write a script to find 3rd smallest element in the sorted matrix. + +Example 1 +Input: @matrix = ([3, 1, 2], [5, 2, 4], [0, 1, 3]) +The sorted list of the given matrix: 0, 1, 1, 2, 2, 3, 3, 4, 5. +The 3rd smallest of the sorted list is 1. +Output: 1 + +Example 2 +Input: @matrix = ([2, 1], [4, 5]) +The sorted list of the given matrix: 1, 2, 4, 5. +The 3rd smallest of the sorted list is 4. +Output: 4 + +Example 3 +Input: @matrix = ([1, 0, 3], [0, 0, 0], [1, 2, 1]) +The sorted list of the given matrix: 0, 0, 0, 0, 1, 1, 1, 2, 3. +The 3rd smallest of the sorted list is 0. +Output: 0 + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +To get the 3rd-smallest member of a matrix, just dump the rows into an array, sort it, and print element 2. + +-------------------------------------------------------------------------------------------------------------- +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 arrays of real numbers in proper Perl syntax, like so: +./ch-1.pl '([[13,0.17,-96.3],[-8,3.82,11],[2,6,4]], [[-83.7,-42,-57],[-99,478,952],[113,127,121.56]])' + +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 numerically_sort_matrix ($mref) { + my @unsorted = (); + my @sorted = (); + for my $rref (@$mref) { + push @unsorted, @$rref; + } + @sorted = sort {$a<=>$b} @unsorted; + return @sorted; +} + +# ------------------------------------------------------------------------------------------------------------ +# DEFAULT INPUTS: +my @arrays = +( + [ + [3, 1, 2], + [5, 2, 4], + [0, 1, 3], + ], + [ + [2, 1], + [4, 5], + ], + [ + [1, 0, 3], + [0, 0, 0], + [1, 2, 1], + ], +); + +# ------------------------------------------------------------------------------------------------------------ +# NON-DEFAULT INPUTS: +if (@ARGV) {@arrays = eval($ARGV[0]);} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +{ # begin main + my $t0 = time; + for my $mref (@arrays) { + my @sorted = numerically_sort_matrix($mref); + say ''; + say 'matrix:'; + for my $rref (@$mref) { + say "[@$rref],"; + } + say "third-smallest = $sorted[2]"; + } + my $µs = 1000000 * (time - $t0); + printf("\nExecution time was %.3fµs.\n", $µs); + exit 0; +} # end main diff --git a/challenge-217/robbie-hatley/perl/ch-2.pl b/challenge-217/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..94a9c86353 --- /dev/null +++ b/challenge-217/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,115 @@ +#! /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-2.pl +Solutions in Perl for The Weekly Challenge 217-2. +Written by Robbie Hatley on Wed May 17, 2023. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 2: Max Number +Submitted by: Mohammad S Anwar +You are given a list of positive integers. Write a script to concatenate the integers to form the highest +possible value. + +Example 1: +Input: @list = (1, 23) +Output: 231 + +Example 2: +Input: @list = (10, 3, 2) +Output: 3210 + +Example 3: +Input: @list = (31, 2, 4, 10) +Output: 431210 + +Example 4: +Input: @list = (5, 11, 4, 1, 2) +Output: 542111 + +Example 5: +Input: @list = (1, 10) +Output: 110 + + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +I'll use the "permutations" feature of CPAN module "Math::Combinatorics", rather than try to create a +special "sort" of the numbers for "max value on concatenation". Less-efficient, but MUCH less headachey. + +-------------------------------------------------------------------------------------------------------------- +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 positive integers in proper Perl syntax, like so: +./ch-2.pl '([13,7,96,8,3,11], [2,6,83,17,7], [478,113,73,14])' + +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 Math::Combinatorics; +use List::Util 'max'; +$"=', '; + +# ------------------------------------------------------------------------------------------------------------ +# SUBROUTINES: +sub concatenate ($aref) { + local $"=''; + return 0+"@$aref"; +} + +sub maxcat ($aref) { + my $combs = Math::Combinatorics->new(count => scalar(@$aref), data => $aref); + my @cats; + while ( my @perm = $combs->next_permutation ) { + push @cats, concatenate(\@perm); + } + return max @cats; +} + +# ------------------------------------------------------------------------------------------------------------ +# DEFAULT INPUTS: +my @arrays = +( + [1, 23], + [10, 3, 2], + [31, 2, 4, 10], + [5, 11, 4, 1, 2], + [1, 10], +); + +# ------------------------------------------------------------------------------------------------------------ +# NON-DEFAULT INPUTS: +if (@ARGV) {@arrays = eval($ARGV[0]);} + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +{ # begin main + my $t0 = time; + for my $aref (@arrays) { + my $maxcat = maxcat($aref); + say ''; + say "array = (@$aref)"; + say "maxcat = $maxcat"; + } + my $µs = 1000000 * (time - $t0); + printf("\nExecution time was %.3fµs.\n", $µs); + exit 0; +} # end main |
