Perl Weekly Challenge 332.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 332.
Task 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"
Assuming a well formed input I can split at the separating dashes,
convert to binary with sprintf
and join again, yielding a
short one-liner.
Examples:
perl -E '
say "$_ -> ", join "-", map {sprintf "%b", $_} split "-" for @ARGV
' 2025-07-26 2000-02-02 2024-12-31
Results:
2025-07-26 -> 11111101001-111-11010
2000-02-02 -> 11111010000-10-10
2024-12-31 -> 11111101000-1100-11111
For the full code I add some checks:
1 # Perl weekly challenge 332
2 # Task 1: Binary Date
3 #
4 # See https://wlmb.github.io/2025/07/26/PWC332/#task-1-binary-date
5 use v5.36;
6 use feature 'try';
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 D1 D2...
9 to map the ISO dates Dn in the format YYYY-MM-DD to binary.
10 FIN
11 my @days=(0,31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
12 for(@ARGV){
13 try {
14 die "Invalid format: $_" unless /^(\d{4})-(\d{2})-(\d{2})$/;
15 my ($year, $month, $day)=($1, $2, $3);
16 die "Invalid month: $_" unless 1<=$month<=12;
17 die "Invalid day: $_" unless 1<=$day<=$days[$month];
18 die "Invalid day: $_" if $month==2 && $day==29 && !leap($year);
19 say "$_ -> ", join "-", map {sprintf "%b", $_} ($year, $month, $day);
20 }
21 catch($e){
22 say $e;
23 }
24 }
25 sub leap($year){
26 return $year%400==0 || $year%4==0 && $year%100 != 0;
27 }
Example:
./ch-1.pl 2025-07-26 2000-02-02 2024-12-31 2024-13-31 2024-12-32 2024-02-29 2023-02-29
Results:
2025-07-26 -> 11111101001-111-11010
2000-02-02 -> 11111010000-10-10
2024-12-31 -> 11111101000-1100-11111
Invalid month: 2024-13-31 at ./ch-1.pl line 17.
Invalid day: 2024-12-32 at ./ch-1.pl line 18.
2024-02-29 -> 11111101000-10-11101
Invalid day: 2023-02-29 at ./ch-1.pl line 19.
Task 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: $str = "weekly"
Output: false
w: 1 time
e: 2 times
k: 1 time
l: 1 time
y: 1 time
The letter 'e' appeared 2 times i.e. even.
Example 2
Input: $str = "perl"
Output: true
Example 3
Input: $source = "challenge"
Output: false
I simply count letters and use modular arithmetic to check parity. I use all
from
List::Util
. The result fits a one-liner.
Examples:
perl -MList::Util=all -E '
for(@ARGV){my %c; $c{$_}++ for split ""; say "$_ -> ", (all{$_%2} values %c)?"T":"F"}
' weekly perl challenge
Results:
weekly -> F
perl -> T
challenge -> F
The full code is:
1 # Perl weekly challenge 332
2 # Task 2: Odd Letters
3 #
4 # See https://wlmb.github.io/2025/07/26/PWC332/#task-2-odd-letters
5 use v5.36;
6 use List::Util qw(all);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 W1 W2...
9 to check if all letters in word Wn appear an odd number of times
10 FIN
11 for(@ARGV){
12 my %count;
13 $count{$_}++ for split "";
14 say "$_ -> ", (all{$_%2} values %count)?"True":"False"
15 }
Example:
./ch-2.pl weekly perl challenge
Results:
weekly -> False
perl -> True
challenge -> False
/;
Written on July 26, 2025