diff options
| author | Bob Lied <boblied+github@gmail.com> | 2025-07-26 11:36:54 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2025-07-26 11:36:54 -0500 |
| commit | c2abd199cd4148d6d01c50d3a233e3bc8fd220c1 (patch) | |
| tree | b7a340915fc5150cdff157ee8a90f3a3f8ef76b3 | |
| parent | 1ff2c9796a511d63231d3757acb27e4046a91fb2 (diff) | |
| download | perlweeklychallenge-club-c2abd199cd4148d6d01c50d3a233e3bc8fd220c1.tar.gz perlweeklychallenge-club-c2abd199cd4148d6d01c50d3a233e3bc8fd220c1.tar.bz2 perlweeklychallenge-club-c2abd199cd4148d6d01c50d3a233e3bc8fd220c1.zip | |
Week 332 solutions
| -rw-r--r-- | challenge-332/bob-lied/README.md | 6 | ||||
| -rw-r--r-- | challenge-332/bob-lied/perl/ch-1.pl | 54 | ||||
| -rw-r--r-- | challenge-332/bob-lied/perl/ch-2.pl | 56 |
3 files changed, 113 insertions, 3 deletions
diff --git a/challenge-332/bob-lied/README.md b/challenge-332/bob-lied/README.md index b995ebc8b4..6f498f7848 100644 --- a/challenge-332/bob-lied/README.md +++ b/challenge-332/bob-lied/README.md @@ -1,4 +1,4 @@ -# Solutions to weekly challenge 330 by Bob Lied +# Solutions to weekly challenge 332 by Bob Lied -## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-330/) -## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-330/bob-lied) +## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-332/) +## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-332/bob-lied) diff --git a/challenge-332/bob-lied/perl/ch-1.pl b/challenge-332/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..92bc0a031e --- /dev/null +++ b/challenge-332/bob-lied/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2025, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 332 Task 1 Binary Date +#============================================================================= +# 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" +#============================================================================= + +use v5.40; + + +use Getopt::Long; +my $Verbose = false; +my $DoTest = 0; + +GetOptions("test" => \$DoTest); +my $logger; +{ + use Log::Log4perl qw(:easy); + Log::Log4perl->easy_init({ level => ($Verbose ? $DEBUG : $INFO ), + layout => "%d{HH:mm:ss.SSS} %p{1} %m%n" }); + $logger = Log::Log4perl->get_logger(); +} +#============================================================================= + +exit(!runTest()) if $DoTest; + +say binaryDate($_) for @ARGV; + +#============================================================================= +sub binaryDate($date) +{ + return sprintf("%b-%b-%b", split(/-/, $date)); +} + +sub runTest +{ + use Test2::V0; + + is( binaryDate("2025-07-26"), "11111101001-111-11010", "Example 1"); + is( binaryDate("2000-02-02"), "11111010000-10-10", "Example 2"); + is( binaryDate("2024-12-31"), "11111101000-1100-11111", "Example 3"); + + done_testing; +} diff --git a/challenge-332/bob-lied/perl/ch-2.pl b/challenge-332/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..9ec0ddda06 --- /dev/null +++ b/challenge-332/bob-lied/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2025, Bob Lied +#============================================================================= +# ch-2.pl Perl Weekly Challenge 332 Task 2 Odd Letters +#============================================================================= +# 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 +# Example 2 Input: $str = "perl" # Output: true +# Example 3 Input: $source = "challenge" # Output: false +#============================================================================= + +use v5.42; +use feature 'keyword_all'; +no warnings 'experimental::keyword_all'; + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +my $logger; +{ + use Log::Log4perl qw(:easy); + Log::Log4perl->easy_init({ level => ($Verbose ? $DEBUG : $INFO ), + layout => "%d{HH:mm:ss.SSS} %p{1} %m%n" }); + $logger = Log::Log4perl->get_logger(); +} +#============================================================================= + +exit(!runTest()) if $DoTest; + +say (oddLetter($_) ? "true" : "false") for @ARGV; + +#============================================================================= +sub oddLetter($str) +{ + my %letter; + $letter{$_}++ for split(//, $str); + return all { $_ % 2 } values %letter; +} + +sub runTest +{ + use Test2::V0; + + is( oddLetter("weekly"), false, "Example 1"); + is( oddLetter("perl"), true, "Example 2"); + is( oddLetter("challenge"), false, "Example 3"); + + is( oddLetter("xaaabbbbb"), true, "More than one"); + + done_testing; +} |
