diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-10 17:20:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-10 17:20:55 +0100 |
| commit | ea3c13ee9b68a723d09fc3ea665e677693b46b98 (patch) | |
| tree | 71f0081a013d14eda7e95929de1677795150a80d | |
| parent | 2689212b61a5b1b9683e6e736b1244d78346c025 (diff) | |
| parent | 8fc798281a4068d075b11ce51fd82f7d182d0393 (diff) | |
| download | perlweeklychallenge-club-ea3c13ee9b68a723d09fc3ea665e677693b46b98.tar.gz perlweeklychallenge-club-ea3c13ee9b68a723d09fc3ea665e677693b46b98.tar.bz2 perlweeklychallenge-club-ea3c13ee9b68a723d09fc3ea665e677693b46b98.zip | |
Merge pull request #12825 from boblied/w342
Week 342 solutions from Bob Lied
| -rw-r--r-- | challenge-342/bob-lied/README.md | 8 | ||||
| -rw-r--r-- | challenge-342/bob-lied/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-342/bob-lied/perl/ch-1.pl | 83 | ||||
| -rw-r--r-- | challenge-342/bob-lied/perl/ch-2.pl | 92 |
4 files changed, 180 insertions, 4 deletions
diff --git a/challenge-342/bob-lied/README.md b/challenge-342/bob-lied/README.md index e3e8ac2801..98b149c185 100644 --- a/challenge-342/bob-lied/README.md +++ b/challenge-342/bob-lied/README.md @@ -1,5 +1,5 @@ -# Solutions to weekly challenge 341 by Bob Lied +# Solutions to weekly challenge 342 by Bob Lied -## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-341/) -## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-341/bob-lied) -[Blog](https://dev.to/boblied/) +## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-342/) +## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-342/bob-lied) +[Blog](https://dev.to/boblied/pwc-342-balance-4eh4) diff --git a/challenge-342/bob-lied/blog.txt b/challenge-342/bob-lied/blog.txt new file mode 100644 index 0000000000..1d7ff08464 --- /dev/null +++ b/challenge-342/bob-lied/blog.txt @@ -0,0 +1 @@ +https://dev.to/boblied/pwc-342-balance-4eh4 diff --git a/challenge-342/bob-lied/perl/ch-1.pl b/challenge-342/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..c5c13ed1df --- /dev/null +++ b/challenge-342/bob-lied/perl/ch-1.pl @@ -0,0 +1,83 @@ +#!/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 342 Task 1 Balance String +#============================================================================= +# You are given a string made up of lowercase English letters and digits only. +# Write a script to format the give string where no letter is followed by +# another letter and no digit is followed by another digit. If there are +# multiple valid rearrangements, always return the lexicographically smallest +# one. Return empty string if it is impossible to format the string. +# Example 1 Input: $str = "a0b1c2" +# Output: "0a1b2c" +# Example 2 Input: $str = "abc12" +# Output: "a1b2c" +# Example 3 Input: $str = "0a2b1c3" +# Output: "0a1b2c3" +# Example 4 Input: $str = "1a23" +# Output: "" +# Example 5 Input: $str = "ab123" +# Output: "1a2b3" +#============================================================================= + +use v5.42; + + +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 balance($_) for @ARGV; + +#============================================================================= +sub balance($str) +{ + use List::MoreUtils qw/part mesh/; + + my @p = part { index("0123456789", $_) >= 0 } sort split(//, $str); + # p[0] is letters, p[1] is digits + + return "" if abs($p[0]->$#* - $p[1]->$#*) > 1; + + return join "", grep { defined } (( $p[0]->$#* > $p[1]->$#* ) + ? mesh($p[0]->@*, $p[1]->@*) + : mesh($p[1]->@*, $p[0]->@*)); +} + +sub runTest +{ + use Test2::V0; + + is( balance("a0b1c2"), "0a1b2c", "Example 1"); + is( balance("abc12"), "a1b2c", "Example 2"); + is( balance("0a2b1c3"), "0a1b2c3", "Example 3"); + is( balance("1a23"), "", "Example 4"); + is( balance("ab123"), "1a2b3", "Example 5"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} diff --git a/challenge-342/bob-lied/perl/ch-2.pl b/challenge-342/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..622a92c64d --- /dev/null +++ b/challenge-342/bob-lied/perl/ch-2.pl @@ -0,0 +1,92 @@ +#!/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 342 Task 2 Max Score +#============================================================================= +# You are given a string, $str, containing 0 and 1 only. +# Write a script to return the max score after splitting the string into two +# non-empty substrings. The score after splitting a string is the number of +# zeros in the left substring plus the number of ones in the right substring. +# Example 1 Input: $str = "0011" +# Output: 4 +# left = "00", right = "11" => 2 + 2 => 4 +# Example 2 Input: $str = "0000" +# Output: 3 +# left = "000", right = "0" => 3 + 0 => 3 +# Example 3 Input: $str = "1111" +# Output: 3 +# left = "1", right = "111" => 0 + 3 => 3 +# Example 4 Input: $str = "0101" +# Output: 3 +# left = "0", right = "101" => 1 + 2 => 3 +# left = "010", right = "1" => 2 + 1 => 3 +# Example 5 Input: $str = "011101" +# Output: 5 +# left = "0", right = "11101" => 1 + 4 => 5 +#============================================================================= + +use v5.42; + + +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 score($_) for @ARGV; + +#============================================================================= +sub score($str) +{ + use List::Util qw/sum/; + my @b = split(//, $str); + my $right = sum(@b) - $b[0]; + my $left = (shift(@b) == 0 ? 1 : 0); + pop(@b); + my $best = $left + $right; + + for ( @b ) + { + if ( $_ ) { $right-- } else { $left++ } + my $s = $left + $right; + $best = $s if $s > $best; + } + return $best; +} + +sub runTest +{ + use Test2::V0; + + is( score("0011" ), 4, "Example 1"); + is( score("0000" ), 3, "Example 2"); + is( score("1111" ), 3, "Example 3"); + is( score("0101" ), 3, "Example 4"); + is( score("011101"), 5, "Example 5"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} |
