aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobbie-hatley <Robbie.Hatley@gmail.com>2025-07-30 00:24:03 -0700
committerrobbie-hatley <Robbie.Hatley@gmail.com>2025-07-30 00:24:03 -0700
commit13ee43870b8e13efc04395e3f4deb8ff080e10f7 (patch)
tree37023f6581e6a6c2865de86902cb32df2a7aff3a
parent1ff2c9796a511d63231d3757acb27e4046a91fb2 (diff)
downloadperlweeklychallenge-club-13ee43870b8e13efc04395e3f4deb8ff080e10f7.tar.gz
perlweeklychallenge-club-13ee43870b8e13efc04395e3f4deb8ff080e10f7.tar.bz2
perlweeklychallenge-club-13ee43870b8e13efc04395e3f4deb8ff080e10f7.zip
Robbie Hatley's solutions, in Perl, for The Weekly Challenge #332.
-rw-r--r--challenge-332/robbie-hatley/blog.txt1
-rwxr-xr-xchallenge-332/robbie-hatley/perl/ch-1.pl71
-rwxr-xr-xchallenge-332/robbie-hatley/perl/ch-2.pl74
3 files changed, 146 insertions, 0 deletions
diff --git a/challenge-332/robbie-hatley/blog.txt b/challenge-332/robbie-hatley/blog.txt
new file mode 100644
index 0000000000..cf1e3af45d
--- /dev/null
+++ b/challenge-332/robbie-hatley/blog.txt
@@ -0,0 +1 @@
+https://hatley-software.blogspot.com/2025/07/robbie-hatleys-solutions-in-perl-for_29.html \ No newline at end of file
diff --git a/challenge-332/robbie-hatley/perl/ch-1.pl b/challenge-332/robbie-hatley/perl/ch-1.pl
new file mode 100755
index 0000000000..41bc0b5500
--- /dev/null
+++ b/challenge-332/robbie-hatley/perl/ch-1.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 332-1,
+written by Robbie Hatley on Mon Jul 28, 2025.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 332-1: Binary Date
+Submitted by: Mohammad Sajid Anwar
+You are given a date in the format YYYY-MM-DD.
+Write a script to convert it into binary date.
+
+Example #1:
+Input: $date = "2025-07-26"
+Output: "11111101001-111-11010"
+
+Example #2:
+Input: $date = "2000-02-02"
+Output: "11111010000-10-10"
+
+Example #3:
+Input: $date = "2024-12-31"
+Output: "11111101000-1100-11111"
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+To solve this, I'll make two subroutines: "dec2bin" which converts positive integers expressed as strings of
+decimal digits into strings of binary digits, and "date2bin" which changes all clusters of digits in a string
+to their binary equivalents by calling "dec2bin" in "s/(\d+)/dec2bin($1)/egr".
+
+--------------------------------------------------------------------------------------------------------------
+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 date strings, in proper Perl syntax, like so:
+
+./ch-1.pl '("837-11-19", "2026-9-28")'
+
+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;
+
+ # Convert decimal numbers to binary:
+ sub dec2bin ($dec) {sprintf("%b", 0+$dec)}
+
+ # Convert decimal substrings to binary:
+ sub date2bin ($date) {$date =~ s/(\d+)/dec2bin($1)/egr}
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @dates = @ARGV ? eval($ARGV[0]) : ("2025-07-26", "2000-02-02", "2024-12-31" );
+# Expected outputs : ("11111101001-111-11010", "11111010000-10-10", "11111101000-1100-11111")
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+$"=', ';
+for my $date (@dates) {
+ say '';
+ say "Decimal date = $date";
+ my $bd = date2bin($date);
+ say "Binary date = $bd";
+}
diff --git a/challenge-332/robbie-hatley/perl/ch-2.pl b/challenge-332/robbie-hatley/perl/ch-2.pl
new file mode 100755
index 0000000000..06c6702faa
--- /dev/null
+++ b/challenge-332/robbie-hatley/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+
+=pod
+
+--------------------------------------------------------------------------------------------------------------
+TITLE AND ATTRIBUTION:
+Solutions in Perl for The Weekly Challenge 332-2,
+written by Robbie Hatley on Mon Jul 28, 2025.
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM DESCRIPTION:
+Task 332-2: Odd Letters
+Submitted by: Mohammad Sajid Anwar
+You are given a string. Write a script to find out if each
+letter in the given string appeared odd number of times.
+
+Example #1:
+Input: "weekly"
+Output: false
+
+Example #2:
+Input: "Perl"
+Output: true
+
+Example #3:
+Input: "challenge"
+Output: false
+
+--------------------------------------------------------------------------------------------------------------
+PROBLEM NOTES:
+I'll use a hash to keep track of the number of times each letter appears, then I'll use the "none" function
+from CPAN module "List::Util" to determine if none of the keys of the hash have even values.
+
+--------------------------------------------------------------------------------------------------------------
+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 '("ratification", "undue", "ethanol", "momentum")'
+
+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 List::Util 'none';
+
+ # Are all letter abundances in a string odd?
+ sub odd_letters ($s) {
+ my %h;
+ for (split //,$s) {
+ ++$h{$_} if $_ =~ m/\pL/
+ }
+ none {0==$h{$_}%2} keys %h;
+ }
+
+# ------------------------------------------------------------------------------------------------------------
+# INPUTS:
+my @strings = @ARGV ? eval($ARGV[0]) : ("weekly", "Perl", "challenge");
+# Expected outputs : false true false
+
+# ------------------------------------------------------------------------------------------------------------
+# MAIN BODY OF PROGRAM:
+$"=', ';
+for my $string (@strings) {
+ say '';
+ say "String = $string";
+ my $true_false = odd_letters($string) ? 'true' : 'false';
+ say "Odd letters? $true_false";
+}