diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2025-05-20 16:06:01 -0700 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2025-05-20 16:06:01 -0700 |
| commit | 157ab9de5fc5024e1256ff1620877036d3cee908 (patch) | |
| tree | 95f26c87f327a8037fff18ed60d9f9fd8b537ead | |
| parent | aaaa94bd6d0f97e4d2ec2abe62588d264491afca (diff) | |
| download | perlweeklychallenge-club-157ab9de5fc5024e1256ff1620877036d3cee908.tar.gz perlweeklychallenge-club-157ab9de5fc5024e1256ff1620877036d3cee908.tar.bz2 perlweeklychallenge-club-157ab9de5fc5024e1256ff1620877036d3cee908.zip | |
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #322.
| -rw-r--r-- | challenge-322/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-322/robbie-hatley/perl/ch-1.pl | 84 | ||||
| -rwxr-xr-x | challenge-322/robbie-hatley/perl/ch-2.pl | 84 |
3 files changed, 169 insertions, 0 deletions
diff --git a/challenge-322/robbie-hatley/blog.txt b/challenge-322/robbie-hatley/blog.txt new file mode 100644 index 0000000000..9c1034b3c5 --- /dev/null +++ b/challenge-322/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2025/05/robbie-hatleys-solutions-in-perl-for_20.html diff --git a/challenge-322/robbie-hatley/perl/ch-1.pl b/challenge-322/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..356f1dae43 --- /dev/null +++ b/challenge-322/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,84 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 322-1, +written by Robbie Hatley on Tue May 20, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 322-1: String Format +Submitted by: Mohammad Sajid Anwar +You are given a string and a positive integer. Write a script to +format the string, removing any dashes, in groups of size given +by the integer. The first group can be smaller than the integer +but should have at least one character. Groups should be +separated by dashes. + +Example #1: +Input: $str = "ABC-D-E-F", $i = 3 +Output: "ABC-DEF" + +Example #2: +Input: $str = "A-BC-D-E", $i = 2 +Output: "A-BC-DE" + +Example #3: +Input: $str = "-A-B-CD-E", $i = 4 +Output: "A-BCDE" + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +I first strip-out all the hypens, then calculate the "remainder" using modular arithmetic: + remainder = length(string) % period +I then build an array of output chunks, the first chunk being the first "remainder" characters snipped from +the left of the stripped string, then each additional chunk being the next "period" characters snipped from +the left of the stripped string. Finally, I join all chunks together with "-" and return the result. + +-------------------------------------------------------------------------------------------------------------- +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, each inner array containing one double-quoted strin and one positive integer, +in proper Perl syntax, like so: + +./ch-1.pl '(["rata-toui-lle", 3],["-ind-est-ruc-tib-le-", 4])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use utf8::all; + + # Use hyphens to group the non-hyphen characters of a given string + # in groups of a given period, with any remainder at the beginning: + sub string_format ($aref) { + my $raw = $$aref[0]; # Raw string. + my $period = $$aref[1]; # Period. + my $stripped = $raw =~ s/-//gr; # Strip hyphens. + my $remainder = length($stripped) % $period; # How many leftover chars? + my @substrings = (); # Groups of period $period. + $remainder and push @substrings, substr $stripped, 0, $remainder, ''; # Leftovers go at beginning. + while (length($stripped) > 0) { # While we still have characters, + push @substrings, substr $stripped, 0, $period, ''} # add groups of period $period. + return join '-', @substrings} # Paste substrings together w "-". + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : (["ABC-D-E-F", 3], ["A-BC-D-E", 2], ["-A-B-CD-E", 4]); +# Expected outputs : "ABC-DEF" "A-BC-DE" "A-BCDE" + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + say ''; + say "Original string = \"$$aref[0]\"; period = $$aref[1]."; + my $reform = string_format($aref); + say "Reformed string = \"$reform\"."; +} diff --git a/challenge-322/robbie-hatley/perl/ch-2.pl b/challenge-322/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..80ab081cbd --- /dev/null +++ b/challenge-322/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,84 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 322-2, +written by Robbie Hatley on Tue May 20, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 322-2: Rank Array +Submitted by: Mohammad Sajid Anwar +You are given an array of integers. Write a script to return an +array of the ranks of each element: the lowest value has rank 1, +next lowest rank 2, etc. If two elements are the same then they +share the same rank. + +Example #1: +Input: @ints = (55, 22, 44, 33) +Output: (4, 1, 3, 2) + +Example #2: +Input: @ints = (10, 10, 10) +Output: (1, 1, 1) + +Example #3: +Input: @ints = (5, 1, 1, 4, 3) +Output: (4, 1, 1, 3, 2) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +I solve this problem by first making a copy of the original array containing its numbers in numerically +increasing order. Then I use a hash to record the "rank" of each number as I iterate through the numbers +in increasing order. Then then I return a list of the "ranks" for the numbers in the original array. + +-------------------------------------------------------------------------------------------------------------- +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 integers, in proper Perl syntax, like so: + +./ch-2.pl '([87,22,64,31,55],[-17,10,-8,7,63,-42])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + use utf8::all; + + # Given an array of numbers, + # print corresponding array of ranks: + sub rank_array ($aref) { + my @sorted = sort {$a<=>$b} @$aref; # Sort numbers in ascending order. + my $max = $sorted[0]; # First number is maximum seen so far. + my $rank = 1; # First number is least so has rank 1. + my %hash; # Make a hash of number ranks + for my $number (@sorted) { # For each number, least to greatest: + if ($number > $max) { # If number > max: + $max = $number; # Update max + ++$rank} # and increment rank. + $hash{$number} = $rank} # Record this number's rank in hash. + my @ranks; # Make an array "@ranks" for output. + for my $number (@$aref) { # For each number in original array, + push @ranks, $hash{$number}} # append number's rank to "@ranks". + return @ranks} # Return ranks. + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : ([55, 22, 44, 33], [10, 10, 10], [5, 1, 1, 4, 3]); +# Expected outputs : (4, 1, 3, 2) (1, 1, 1) (4, 1, 1, 3, 2) + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + say ''; + say "Array: (@$aref)"; + my @ranks = rank_array($aref); + say "Ranks: (@ranks)"; +} |
