diff options
| author | Bob Lied <boblied+github@gmail.com> | 2025-07-03 09:57:11 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2025-07-03 09:57:11 -0500 |
| commit | 8ed259a8992aa04f7a7c6b01eecf122bc0c9eba7 (patch) | |
| tree | 039a98ccd9be28cd88d2ca9403dc5f68cbaa2c68 | |
| parent | 7daf92e1dd4a2726fc578e87b6364ba4db3d5ad9 (diff) | |
| download | perlweeklychallenge-club-8ed259a8992aa04f7a7c6b01eecf122bc0c9eba7.tar.gz perlweeklychallenge-club-8ed259a8992aa04f7a7c6b01eecf122bc0c9eba7.tar.bz2 perlweeklychallenge-club-8ed259a8992aa04f7a7c6b01eecf122bc0c9eba7.zip | |
Week 328 solutions
| -rw-r--r-- | challenge-328/bob-lied/README.md | 6 | ||||
| -rw-r--r-- | challenge-328/bob-lied/perl/ch-1.pl | 89 | ||||
| -rw-r--r-- | challenge-328/bob-lied/perl/ch-2.pl | 75 |
3 files changed, 167 insertions, 3 deletions
diff --git a/challenge-328/bob-lied/README.md b/challenge-328/bob-lied/README.md index 13e20a4e1e..cd437b9597 100644 --- a/challenge-328/bob-lied/README.md +++ b/challenge-328/bob-lied/README.md @@ -1,4 +1,4 @@ -# Solutions to weekly challenge 327 by Bob Lied +# Solutions to weekly challenge 328 by Bob Lied -## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-327/) -## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-327/bob-lied) +## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-328/) +## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-328/bob-lied) diff --git a/challenge-328/bob-lied/perl/ch-1.pl b/challenge-328/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..cf78001a3a --- /dev/null +++ b/challenge-328/bob-lied/perl/ch-1.pl @@ -0,0 +1,89 @@ +#!/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 328 Task 1 Replace all ? +#============================================================================= +# You are given a string containing only lower case English letters and ?. +# Write a script to replace all ? in the given string so that the string +# doesn’t contain consecutive repeating characters. +# Example 1 Input: $str = "a?z" +# Output: "abz" +# There can be many strings, one of them is "abz". The choices are 'a' +# to 'z' but we can't use either 'a' or 'z' to replace the '?'. +# Example 2 Input: $str = "pe?k" +# Output: "peak" +# Example 3 Input: $str = "gra?te" +# Output: "grabte" +#============================================================================= + +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(); +} +#============================================================================= + +my %replace = ( "a?a" => "b", "a?b" => "c", + "b?b" => "a", "b?a" => "c", ); + +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say replaceQ($_) for @ARGV; + +#============================================================================= +sub replaceQ($str) +{ + my $pos = 0; + while ( ($pos = index($str, "?", $pos)) != -1 ) + { + my $before = substr($str, $pos-1, 1); + my $after = substr($str, $pos+1, 1); + + my $instead = ( $before eq 'a' || $after eq 'a' ) ? 'b' : 'a'; + $instead = 'c' if ( $before eq $instead || $after eq $instead ); + + substr($str, $pos, 1, $instead); + } + return $str; +} + +sub runTest +{ + use Test2::V0; + + is( replaceQ("a?z" ), "abz" , "Example 1"); + is( replaceQ("pe?k" ), "peak" , "Example 2"); + is( replaceQ("gra?te"), "grabte", "Example 3"); + + is( replaceQ("abcde" ), "abcde" , "No question marks"); + is( replaceQ("?abcd" ), "babcd" , "Leading"); + is( replaceQ("abcd?" ), "abcda" , "Trailing"); + is( replaceQ("a?b" ), "acb" , "Can't be a or b"); + is( replaceQ("x???z" ), "xabaz" , "Multiple question marks"); + + done_testing; +} + +sub runBenchmark($repeat) +{ +use Benchmark qw/cmpthese/; + +cmpthese($repeat, { +label => sub { }, +}); +} + diff --git a/challenge-328/bob-lied/perl/ch-2.pl b/challenge-328/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..476d78d97e --- /dev/null +++ b/challenge-328/bob-lied/perl/ch-2.pl @@ -0,0 +1,75 @@ +#!/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 328 Task 2 Good String +#============================================================================= +# You are given a string made up of lower and upper case English letters only. +# Write a script to return the good string of the given string. A string is +# called good string if it doesn’t have two adjacent same characters, one in +# upper case and other is lower case. +# Example 1 Input: $str = "WeEeekly" +# Output: "Weekly" +# We can remove either, "eE" or "Ee" to make it good. +# Example 2 Input: $str = "abBAdD" +# Output: "" +# We remove "bB" first: "aAdD". Then we remove "aA": "dD" Finally remove "dD". +# Example 3 Input: $str = "abc" +# Output: "abc" +#============================================================================= + +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(); +} +#============================================================================= + +# Generate the regular expression "aA|Aa|bB|Bb|...|Zz" +my $ToBeRemoved = join("|", map { my $uc=uc($_); "$_$uc", "$uc$_" } 'a' .. 'z'); + +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say enGooden($_) for @ARGV; + +#============================================================================= +sub enGooden($str) +{ + while ( $str =~ s/$ToBeRemoved//g ) { } + return $str; +} + +sub runTest +{ + use Test2::V0; + + is( enGooden("WeEeekly"), "Weekly", "Example 1"); + is( enGooden("abBAdD"), "", "Example 2"); + is( enGooden("abc"), "abc", "Example 3"); + + is( enGooden("xAadBbDyCczdD"), "xyz", "upper-loeer combo"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} |
