diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-07-13 23:41:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-13 23:41:29 +0100 |
| commit | 0fde42bca41781b59a7fa82466b146057ea252c2 (patch) | |
| tree | b44f9cd59474d5733b59b504cc06176c46879954 | |
| parent | abc5eb39234a29af23c60847737061611c1791e8 (diff) | |
| parent | 0f18774ddd1a9f3689fb9e6d7767b09f0e9f505c (diff) | |
| download | perlweeklychallenge-club-0fde42bca41781b59a7fa82466b146057ea252c2.tar.gz perlweeklychallenge-club-0fde42bca41781b59a7fa82466b146057ea252c2.tar.bz2 perlweeklychallenge-club-0fde42bca41781b59a7fa82466b146057ea252c2.zip | |
Merge pull request #12327 from boblied/w329
Week 329 solutions from Bob Lied
| -rw-r--r-- | challenge-329/bob-lied/README.md | 6 | ||||
| -rw-r--r-- | challenge-329/bob-lied/perl/ch-1.pl | 72 | ||||
| -rw-r--r-- | challenge-329/bob-lied/perl/ch-2.pl | 86 |
3 files changed, 161 insertions, 3 deletions
diff --git a/challenge-329/bob-lied/README.md b/challenge-329/bob-lied/README.md index cd437b9597..8711e60b9c 100644 --- a/challenge-329/bob-lied/README.md +++ b/challenge-329/bob-lied/README.md @@ -1,4 +1,4 @@ -# Solutions to weekly challenge 328 by Bob Lied +# Solutions to weekly challenge 329 by Bob Lied -## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-328/) -## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-328/bob-lied) +## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-329/) +## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-329/bob-lied) diff --git a/challenge-329/bob-lied/perl/ch-1.pl b/challenge-329/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..70bb1acdbb --- /dev/null +++ b/challenge-329/bob-lied/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/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 329 Task 1 Counter Integers +#============================================================================= +# You are given a string containing only lower case English letters and digits. +# Write a script to replace every non-digit character with a space and then +# return all the distinct integers left. +# Example 1 Input: $str = "the1weekly2challenge2" +# Output: 1, 2 +# 2 is appeared twice, so we count it one only. +# Example 2 Input: $str = "go21od1lu5c7k" +# Output: 21, 1, 5, 7 +# Example 3 Input: $str = "4p3e2r1l" +# Output: 4, 3, 2, 1 +#============================================================================= + +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 join(", ", count($_)->@*) for @ARGV; + +#============================================================================= +sub count($str) +{ + # Preserve order while eliminating repeated elements + my $order = 1; + my %present; + $present{$_} //= $order++ for ( my @ints = $str =~ m/\d+/g ); + + return [ sort { $present{$a} <=> $present{$b} } keys %present ]; +} + +sub runTest +{ + use Test2::V0; + + is( count("the1weekly2challenge2"), [1,2], "Example 1"); + is( count("go21od1lu5c7k"), [21,1,5,7], "Example 2"); + is( count("4p3e2r1l"), [4,3,2,1], "Example 3"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} diff --git a/challenge-329/bob-lied/perl/ch-2.pl b/challenge-329/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..0205c8f8ae --- /dev/null +++ b/challenge-329/bob-lied/perl/ch-2.pl @@ -0,0 +1,86 @@ +#!/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 329 Task 2 Nice String +#============================================================================= +# You are given a string made up of lower and upper case English letters only. +# Write a script to return the longest substring of the givent string which is +# nice. A string is nice if, for every letter of the alphabet that the string +# contains, it appears both in uppercase and lowercase. +# Example 1 Input: $str = "YaaAho" +# Output: "aaA" +# Example 2 Input: $str = "cC" +# Output: "cC" +# Example 3 Input: $str = "A" +# Output: "" +#============================================================================= + +use v5.40; +use English; + +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 nice2($_) for @ARGV; + +#============================================================================= +sub nice($str) +{ + my $longest = 0; + my $nice = ""; + while ( $str =~ m/(.)\1*/ig ) + { + my $c = substr($MATCH, 0, 1); + next unless index($MATCH, uc($c)) != -1 && index($MATCH, lc($c)) != -1; + + my $len = $LAST_MATCH_END[0] - $LAST_MATCH_START[0]; + if ( $len > $longest ) + { + $longest = $len; + $nice = $MATCH; + } + } + return $nice; +} + + +sub runTest +{ + use Test2::V0; + + is( nice("YaaAho"), "aaA", "Example 1"); + is( nice("cC" ), "cC", "Example 2"); + is( nice("A" ), "", "Example 3"); + + is( nice("XlllllY"), "", "No uppercase"); + is( nice("xUUUUUy"), "", "No lowercase"); + is( nice("XaaAAYbBbBbZcC"), "bBbBb", "Multiple possibilities"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} |
