diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-07-11 21:32:11 -0700 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2024-07-11 21:32:11 -0700 |
| commit | e8b069ab2f3ba1904ea6b91c130ceef809d8401c (patch) | |
| tree | d0c6faa36728040da058eaab48300b6484126cf4 | |
| parent | bf4eb71a4e5ef70445e3319aba7e9666235a1ecf (diff) | |
| download | perlweeklychallenge-club-e8b069ab2f3ba1904ea6b91c130ceef809d8401c.tar.gz perlweeklychallenge-club-e8b069ab2f3ba1904ea6b91c130ceef809d8401c.tar.bz2 perlweeklychallenge-club-e8b069ab2f3ba1904ea6b91c130ceef809d8401c.zip | |
Robbie Hatley solutions, in Perl, for The Weekly Challenge #277.
| -rw-r--r-- | challenge-277/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-277/robbie-hatley/perl/ch-1.pl | 112 | ||||
| -rwxr-xr-x | challenge-277/robbie-hatley/perl/ch-2.pl | 83 |
3 files changed, 196 insertions, 0 deletions
diff --git a/challenge-277/robbie-hatley/blog.txt b/challenge-277/robbie-hatley/blog.txt new file mode 100644 index 0000000000..9195bd6d97 --- /dev/null +++ b/challenge-277/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2024/07/robbie-hatleys-solutions-to-weekly.html
\ No newline at end of file diff --git a/challenge-277/robbie-hatley/perl/ch-1.pl b/challenge-277/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..8509b51be7 --- /dev/null +++ b/challenge-277/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,112 @@ +#!/usr/bin/env -S perl -CSDA + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 277-1, +written by Robbie Hatley on Thu Jul 11, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 277-1: Count Common +Submitted by: Mohammad Sajid Anwar +Given two arrays of strings,write a script to return the count +of words which appear once-each in the two arrays. + + # Example 1 input: + [ + ["Perl", "is", "my", "friend"], + ["Perl", "and", "Raku", "are", "friend"], + ], + # Expected output: 2 + # (The words "Perl" and "friend" appear once in each array.) + + # Example 2 input: + [ + ["Perl", "and", "Python", "are", "very", "similar"], + ["Python", "is", "top", "in", "guest", "languages"], + ], + # Expected output: 1 + # (The word "Python" appears once in each array.) + + # Example 3 input: + [ + ["Perl", "is", "imperative", "Lisp", "is", "functional"], + ["Crystal", "is", "similar", "to", "Ruby"], + ], + # Expected output: 0 + # ("is" appears twice in the first array so it doesn't count.) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +I attack this problem by making a hash keyed by words with each value being a 2-element array containing +counts of how many times that word appears in each array. Then I just count how many keys have both elements +of the value equal to 1 (which means the word appears one-each in the two arrays of words). + +-------------------------------------------------------------------------------------------------------------- +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 2 arrays of double-quoted strings, in proper Perl syntax, like so: +./ch-1.pl '([["dog", "pig", "cow"],["bat","cat","pig"]],[["rock","stick","dish"],["metal","grunge","rock"]])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.38; + use utf8; + no warnings 'uninitialized'; + sub count_common ($aref1, $aref2) { + my %counts; + ++$counts{$_}->[0] for @$aref1; + ++$counts{$_}->[1] for @$aref2; + my $count = 0; + for (keys %counts) { + ++$count if 1 == $counts{$_}->[0] + && 1 == $counts{$_}->[1]; + } + $count; + } + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + [ + ["Perl", "is", "my", "friend"], + ["Perl", "and", "Raku", "are", "friend"], + ], + # Expected output: 2 + # (The words "Perl" and "friend" appear once in each array.) + + # Example 2 input: + [ + ["Perl", "and", "Python", "are", "very", "similar"], + ["Python", "is", "top", "in", "guest", "languages"], + ], + # Expected output: 1 + # (The word "Python" appears once in each array.) + + # Example 3 input: + [ + ["Perl", "is", "imperative", "Lisp", "is", "functional"], + ["Crystal", "is", "similar", "to", "Ruby"], + ], + # Expected output: 0 + # ("is" appears twice in the first array so it doesn't count.) +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $aref (@arrays) { + say ''; + my $common = count_common($aref->[0], $aref->[1]); + say "Array1 = (${\join(q(, ), map {qq(\"$_\")} @{$aref->[0]})})"; + say "Array2 = (${\join(q(, ), map {qq(\"$_\")} @{$aref->[1]})})"; + say "$common words appear once-each in the two arrays."; +} diff --git a/challenge-277/robbie-hatley/perl/ch-2.pl b/challenge-277/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..90d9e75cd6 --- /dev/null +++ b/challenge-277/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,83 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 277-2, +written by Robbie Hatley on Thu Jul 11, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 277-2: Strong Pair +Submitted by: Mohammad Sajid Anwar +Given an array of integers, write a script to return the count +of all strong pairs in the given array. A pair of integers +x and y is called a "strong pair" if it satisfies theses +inequalities: 0 < |x - y| and |x - y| < min(x, y). + + # Example 1 input: + [1, 2, 3, 4, 5], + # Expected output: 4 + # (Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5).) + + # Example 2 input: + [5, 7, 1, 7], + # Expected output: 1 + # (Strong Pairs: (5, 7).) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +To prevent considering duplicate pairs, I make sorted, deduped copies of both arrays, then I use nested ranged +for loops to consider each unique pair, and I use && to and-together the given inequalities to determine how +many pairs are "strong". + +-------------------------------------------------------------------------------------------------------------- +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 '([1,2,3,4,5,6],[21,22,23,24,25,26])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.38; + use List::Util 'uniq'; + sub min ($x,$y) {return($y<$x?$y:$x)} + sub strong ($aref) { + my @uniq = uniq sort {$a<=>$b} @$aref; + my $strong = 0; + for my $i ( 0 .. $#uniq - 1 ) { my $x = $$aref[$i]; + for my $j ( $i + 1 .. $#uniq - 0 ) { my $y = $$aref[$j]; + ++$strong if 0 < abs($y-$x) && abs($y-$x) < min($x,$y); + } + } + return $strong; + } + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + [1, 2, 3, 4, 5], + # Expected output: 4 + # (Strong Pairs: (2, 3), (3, 4), (3, 5), (4, 5).) + + # Example 2 input: + [5, 7, 1, 7], + # Expected output: 1 + # (Strong Pairs: (5, 7).) +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $aref (@arrays) { + say ''; + say "Array = (${\join(q(, ), @$aref)})"; + say "Number of strong pairs = ${\strong($aref)}"; +} |
