diff options
| author | Bob Lied <boblied+github@gmail.com> | 2025-06-16 06:38:33 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2025-06-16 06:38:33 -0500 |
| commit | 480d89d8e315b988b77d2b0aab5d79f7561d145c (patch) | |
| tree | 1f3ca3ff967e754808fb5ae7425684091635265f | |
| parent | 26cfae99bb0a2fdf9710bcc51e8abc8d7ed627f6 (diff) | |
| download | perlweeklychallenge-club-480d89d8e315b988b77d2b0aab5d79f7561d145c.tar.gz perlweeklychallenge-club-480d89d8e315b988b77d2b0aab5d79f7561d145c.tar.bz2 perlweeklychallenge-club-480d89d8e315b988b77d2b0aab5d79f7561d145c.zip | |
Week 326 solutions
| -rw-r--r-- | challenge-326/bob-lied/README.md | 6 | ||||
| -rw-r--r-- | challenge-326/bob-lied/perl/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-326/bob-lied/perl/ch-2.pl | 74 |
3 files changed, 129 insertions, 3 deletions
diff --git a/challenge-326/bob-lied/README.md b/challenge-326/bob-lied/README.md index 05d522492e..7152e056f1 100644 --- a/challenge-326/bob-lied/README.md +++ b/challenge-326/bob-lied/README.md @@ -1,4 +1,4 @@ -# Solutions to weekly challenge 325 by Bob Lied +# Solutions to weekly challenge 326 by Bob Lied -## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-325/) -## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-325/bob-lied) +## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-326/) +## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-326/bob-lied) diff --git a/challenge-326/bob-lied/perl/ch-1.pl b/challenge-326/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..8efb949ed0 --- /dev/null +++ b/challenge-326/bob-lied/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/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 326 Task 1 Day of Year +#============================================================================= +# You are given a date in the format YYYY-MM-DD. Write a script to find day +# number of the year that the given date represent. +# Example 1 Input: $date = '2025-02-02' Output: 33 +# The 2nd Feb, 2025 is the 33rd day of the year. +# Example 2 Input: $date = '2025-04-10' Output: 100 +# Example 3 Input: $date = '2025-09-07' Output: 250 +#============================================================================= + +use v5.40; + +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 yearDay($_) for @ARGV; + +#============================================================================= +sub yearDay($date) +{ + use Time::Piece; + return Time::Piece->strptime($date, "%Y-%m-%d")->day_of_year() + 1; +} + +sub runTest +{ + use Test2::V0; + + is( yearDay("2025-02-02"), 33, "Example 1"); + is( yearDay("2025-04-10"), 100, "Example 2"); + is( yearDay("2025-09-07"), 250, "Example 3"); + + done_testing; +} diff --git a/challenge-326/bob-lied/perl/ch-2.pl b/challenge-326/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..5049248670 --- /dev/null +++ b/challenge-326/bob-lied/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/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 326 Task 2 Decompressed List +#============================================================================= +# You are given an array of positive integers having even elements. +# Write a script to to return the decompress list. To decompress, pick +# adjacent pair (i, j) and replace it with j, i times. +# Example 1 Input: @ints = (1, 3, 2, 4) +# Output: (3, 4, 4) +# Pair 1: (1, 3) => 3 one time => (3) +# Pair 2: (2, 4) => 4 two times => (4, 4) +# Example 2 Input: @ints = (1, 1, 2, 2) +# Output: (1, 2, 2) +# Example 3 Input: @ints = (3, 1, 3, 2) +# Output: (1, 1, 1, 2, 2, 2) +#============================================================================= + +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 decompress(@ARGV); + +#============================================================================= +sub decompress(@ints) +{ + my @output; + for my ($repeat, $value) ( @ints ) + { + push @output, ($value) x $repeat; + } + + return \@output; +} + +sub runTest +{ + use Test2::V0; + + is( decompress(1,3,2,4), [3,4,4], "Example 1"); + is( decompress(1,1,2,2), [1,2,2], "Example 2"); + is( decompress(3,1,3,2), [1,1,1,2,2,2], "Example 3"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} |
