diff options
| author | Bob Lied <boblied+github@gmail.com> | 2025-07-20 09:14:15 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2025-07-20 09:14:15 -0500 |
| commit | bba8189c54b515d61d0b179147e1f09b2f951015 (patch) | |
| tree | 85fc00f1c716dd007893a15e08d2d380613d664c | |
| parent | 6345c73edaafe1c1252e99cf8991c8fc27890445 (diff) | |
| download | perlweeklychallenge-club-bba8189c54b515d61d0b179147e1f09b2f951015.tar.gz perlweeklychallenge-club-bba8189c54b515d61d0b179147e1f09b2f951015.tar.bz2 perlweeklychallenge-club-bba8189c54b515d61d0b179147e1f09b2f951015.zip | |
Week 330 solutions
| -rw-r--r-- | challenge-330/bob-lied/README.md | 6 | ||||
| -rw-r--r-- | challenge-330/bob-lied/perl/ch-1.pl | 71 | ||||
| -rw-r--r-- | challenge-330/bob-lied/perl/ch-2.pl | 67 |
3 files changed, 141 insertions, 3 deletions
diff --git a/challenge-330/bob-lied/README.md b/challenge-330/bob-lied/README.md index 8711e60b9c..b995ebc8b4 100644 --- a/challenge-330/bob-lied/README.md +++ b/challenge-330/bob-lied/README.md @@ -1,4 +1,4 @@ -# Solutions to weekly challenge 329 by Bob Lied +# Solutions to weekly challenge 330 by Bob Lied -## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-329/) -## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-329/bob-lied) +## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-330/) +## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-330/bob-lied) diff --git a/challenge-330/bob-lied/perl/ch-1.pl b/challenge-330/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..9d589aec20 --- /dev/null +++ b/challenge-330/bob-lied/perl/ch-1.pl @@ -0,0 +1,71 @@ +#!/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 330 Task 1 Clear Digits +#============================================================================= +# You are given a string containing only lower case English letters and +# digits. Write a script to remove all digits by removing the first digit +# and the closest non-digit character to its left. +# Example 1 Input: $str = "cab12" +# Output: "c" +# Round 1: remove "1" then "b" => "ca2" +# Round 2: remove "2" then "a" => "c" +# Example 2 Input: $str = "xy99" +# Output: "" +# Example 3 Input: $str = "pa1erl" +# Output: "perl" +#============================================================================= + +use v5.40; + + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; +my $Benchmark = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark); +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; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say clearDigits($_) for @ARGV; + +#============================================================================= +sub clearDigits($str) +{ + while ( $str =~ s/(?:^|[a-z])[0-9]//g ) { } + return $str; +} + +sub runTest +{ + use Test2::V0; + + is( clearDigits("cab12" ), "c", "Example 1"); + is( clearDigits("xy99" ), "", "Example 2"); + is( clearDigits("pa1erl"), "perl", "Example 3"); + + is( clearDigits("7abc"), "abc", "Starts with digits"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} diff --git a/challenge-330/bob-lied/perl/ch-2.pl b/challenge-330/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..068dae153d --- /dev/null +++ b/challenge-330/bob-lied/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/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 330 Task 2 Title Capital +#============================================================================= +# You are given a string made up of one or more words separated by a single +# space. Write a script to capitalise the given title. If the word length is +# 1 or 2 then convert the word to lowercase otherwise make the first +# character uppercase and remaining lowercase. +# Example 1 Input: $str = "PERL IS gREAT" +# Output: "Perl is Great" +# Example 2 Input: $str = "THE weekly challenge" +# Output: "The Weekly Challenge" +# Example 3 Input: $str = "YoU ARE A stAR" +# Output: "You Are a Star" +#============================================================================= + +use v5.40; + + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; +my $Benchmark = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark); +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; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say capTitle($_) for @ARGV; + +#============================================================================= +sub capTitle($str) +{ + join(" ", map { length($_) > 2 ? ucfirst($_) : $_ } split(/ /, lc($str) )); +} + +sub runTest +{ + use Test2::V0; + + is( capTitle("PERL IS gREAT" ), "Perl is Great", "Example 1"); + is( capTitle("THE weekly challenge"), "The Weekly Challenge", "Example 2"); + is( capTitle("YoU ARE A sTAR" ), "You Are a Star", "Example 3"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} |
