aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-23 09:06:18 +0100
committerGitHub <noreply@github.com>2024-07-23 09:06:18 +0100
commitbb92ce86feb4b5ab2d082a6ece1b3d4cb4913df6 (patch)
tree6ee62a69f6052f57629fbf9d4de7f0929dc9b07a
parent8696dfc2fa954ab74a8369e15e9a2bf369008ffb (diff)
parent43cf8bdccb016d1ea8db803050cce243931a50fe (diff)
downloadperlweeklychallenge-club-bb92ce86feb4b5ab2d082a6ece1b3d4cb4913df6.tar.gz
perlweeklychallenge-club-bb92ce86feb4b5ab2d082a6ece1b3d4cb4913df6.tar.bz2
perlweeklychallenge-club-bb92ce86feb4b5ab2d082a6ece1b3d4cb4913df6.zip
Merge pull request #10483 from robbie-hatley/rh279
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #279.
-rw-r--r--challenge-279/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-279/robbie-hatley/perl/ch-1.pl97
-rwxr-xr-xchallenge-279/robbie-hatley/perl/ch-2.pl79
3 files changed, 177 insertions, 0 deletions
diff --git a/challenge-279/robbie-hatley/blog.txt b/challenge-279/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..d4967a3810
--- /dev/null
+++ b/challenge-279/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2024/07/robbie-hatleys-solutions-to-weekly_23.html \ No newline at end of file
diff --git a/challenge-279/robbie-hatley/perl/ch-1.pl b/challenge-279/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..8bfb44be55
--- /dev/null
+++ b/challenge-279/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,97 @@
+#!/usr/bin/env -S perl -CSDA
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 279-1,
+written by Robbie Hatley on Mon Jul 22, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 279-1: Sort Letters
+Submitted by: Mohammad Sajid Anwar
+Given two arrays, @letters and @weights, write a script to sort
+@letters based on @weights.
+
+Example 1:
+Input: @letters = ('R', 'E', 'P', 'L')
+ @weights = (3, 2, 1, 4)
+Output: PERL
+
+Example 2:
+Input: @letters = ('A', 'U', 'R', 'K')
+ @weights = (2, 4, 1, 3)
+Output: RAKU
+
+Example 3:
+Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T')
+ @weights = (5, 4, 2, 6, 1, 3)
+Output: PYTHON
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I use List::MoreUtils::Zip6 to make a list of [letter, weight] pairs, sort those pairs by weight,
+then return a join of the letters of the sorted list.
+
+--------------------------------------------------------------------------------------------------------------
+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 two arrays, with each pair of inner arrays consisint of a "letters" array
+followed by a "weights" array, in proper Perl syntax, like so:
+./ch-1.pl '([["F","R","A","N","K"],[4,1,5,2,3]],[["E","B","R","I","O","B"],[6,3,1,5,2,4]])'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+ use v5.36;
+ use List::MoreUtils 'zip6';
+ sub sort_letters :prototype(\@\@) ($letters, $weights) {
+ join '',
+ map {$$_[0]}
+ sort {$$a[1]<=>$$b[1]}
+ zip6 @$letters, @$weights;
+ }
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @arrays = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 input:
+ [
+ ['R', 'E', 'P', 'L'],
+ [3, 2, 1, 4],
+ ],
+ # Expected output: PERL
+
+ # Example 2 input:
+ [
+ ['A', 'U', 'R', 'K'],
+ [2, 4, 1, 3],
+ ],
+ # Expected output: RAKU
+
+ # Example 3 input:
+ [
+ ['O', 'H', 'Y', 'N', 'P', 'T'],
+ [5, 4, 2, 6, 1, 3],
+ ],
+ # Expected output: PYTHON
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+$"=', ';
+for my $aref (@arrays) {
+ my @letters = @{$$aref[0]};
+ my @weights = @{$$aref[1]};
+ my $word = sort_letters(@letters,@weights);
+ say '';
+ say "letters = (@letters)";
+ say "weights = (@weights)";
+ say "Sorted letters = $word";
+}
diff --git a/challenge-279/robbie-hatley/perl/ch-2.pl b/challenge-279/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..d969ea706d
--- /dev/null
+++ b/challenge-279/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/env -S perl -CSDA
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 279-2,
+written by Robbie Hatley on Mon Jul 22, 2024.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 279-2: Split String
+Submitted by: Mohammad Sajid Anwar
+Given a string $str, write a script to split $str into two
+strings, each containing exactly same number of vowels.
+Return true if you can, otherwise return false.
+
+Example 1:
+Input: $str = "perl"
+Ouput: false
+
+Example 2:
+Input: $str = "book"
+Ouput: true
+
+Example 3
+Input: $str = "good morning"
+Ouput: true
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I don't even bother actually splitting strings, since we're only interested in whether the number of vowels
+is even. So I make two subs, "count_vowels" and "can_split", which do as their names suggest. The only
+tricky part is defining what a "vowel" is. For the purposes of this exercise I'll define a "vowel" to be
+any of [aeiouAEIOU] with-or-without combining marks.
+
+--------------------------------------------------------------------------------------------------------------
+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 double-quoted strings in proper Perl syntax, like so:
+./ch-2.pl '("Is Trump a winner?", "Will Kamala be President?")'
+
+Output is to STDOUT and will be each input followed by the corresponding output.
+
+=cut
+
+# ------------------------------------------------------------------------------------------------------------
+# PRAGMAS, MODULES, AND SUBS:
+
+ use v5.36;
+ use Unicode::Normalize 'NFD';
+ sub count_vowels ($string) {scalar map {$_} NFD($string) =~ m/[aeiouAEIOU]/g}
+ sub can_split ($string) {(0==count_vowels($string)%2)?"true":"false"}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @strings = @ARGV ? eval($ARGV[0]) :
+(
+ # Example 1 input:
+ "perl",
+ # Expected ouput: false
+
+ # Example 2:
+ "book",
+ # Expected ouput: true
+
+ # Example 3
+ "good morning",
+ # Expected ouput: true
+);
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+for my $string (@strings) {
+ my $result = can_split($string);
+ say '';
+ say "String = $string";
+ say "Result = $result";
+}