aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-05-06 15:31:12 +0100
committerGitHub <noreply@github.com>2025-05-06 15:31:12 +0100
commit1846ebce5ec35378d7a0a0482f98023265c1e03a (patch)
treeec3b4f1853ccde2924c510019117eaf52818b5f2
parente98c19acbe0cc6e9eab03fc3aa05cc43909cd417 (diff)
parent81f6a292f8902fce7dbd8ff8bfb0985a598c94f9 (diff)
downloadperlweeklychallenge-club-1846ebce5ec35378d7a0a0482f98023265c1e03a.tar.gz
perlweeklychallenge-club-1846ebce5ec35378d7a0a0482f98023265c1e03a.tar.bz2
perlweeklychallenge-club-1846ebce5ec35378d7a0a0482f98023265c1e03a.zip
Merge pull request #11982 from boblied/w320
Week 320 solutions from Bob Lied
-rw-r--r--challenge-320/bob-lied/README.md6
-rw-r--r--challenge-320/bob-lied/perl/ch-1.pl70
-rw-r--r--challenge-320/bob-lied/perl/ch-2.pl80
3 files changed, 153 insertions, 3 deletions
diff --git a/challenge-320/bob-lied/README.md b/challenge-320/bob-lied/README.md
index 0a456ef2a3..95cd170130 100644
--- a/challenge-320/bob-lied/README.md
+++ b/challenge-320/bob-lied/README.md
@@ -1,4 +1,4 @@
-# Solutions to weekly challenge 319 by Bob Lied
+# Solutions to weekly challenge 320 by Bob Lied
-## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-319/)
-## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-319/bob-lied)
+## [PWC](https://perlweeklychallenge.org/blog/perl-weekly-challenge-320/)
+## [GitHub](https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-320/bob-lied)
diff --git a/challenge-320/bob-lied/perl/ch-1.pl b/challenge-320/bob-lied/perl/ch-1.pl
new file mode 100644
index 0000000000..64b7af1c73
--- /dev/null
+++ b/challenge-320/bob-lied/perl/ch-1.pl
@@ -0,0 +1,70 @@
+#!/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 320 Task 1 Maximum Count
+#=============================================================================
+# You are given an array of integers. Write a script to return the maximum
+# between the number of positive and negative integers. Zero is neither
+# positive nor negative.
+# Example 1 Input: @ints = (-3, -2, -1, 1, 2, 3)
+# Output: 3
+# Example 2 Input: @ints = (-2, -1, 0, 0, 1)
+# Output: 2
+# Example 3 Input: @ints = (1, 2, 3, 4)
+# Output: 4
+#=============================================================================
+
+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 maxCount(@ARGV);
+
+#=============================================================================
+sub maxCount(@ints)
+{
+ my @sign = (0,0,0);
+
+ $sign[ $_ <=> 0 ]++ for @ints;
+
+ return $sign[1] > $sign[-1] ? $sign[1] : $sign[-1];
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( maxCount(-3,-2,-1,1,2,3), 3, "Example 1");
+ is( maxCount(-2,-1,0,0,1), 2, "Example 2");
+ is( maxCount(1,2,3,4), 4, "Example 3");
+
+ done_testing;
+}
+
+sub runBenchmark($repeat)
+{
+ use Benchmark qw/cmpthese/;
+
+ cmpthese($repeat, {
+ label => sub { },
+ });
+}
diff --git a/challenge-320/bob-lied/perl/ch-2.pl b/challenge-320/bob-lied/perl/ch-2.pl
new file mode 100644
index 0000000000..3f9a578fdf
--- /dev/null
+++ b/challenge-320/bob-lied/perl/ch-2.pl
@@ -0,0 +1,80 @@
+#!/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 320 Task 2 Sum Difference
+#=============================================================================
+# You are given an array of positive integers. Write a script to return the
+# absolute difference between digit sum and element sum of the given array.
+# Example 1 Input: @ints = (1, 23, 4, 5)
+# Output: 18
+# Element sum: 1 + 23 + 4 + 5 => 33
+# Digit sum: 1 + 2 + 3 + 4 + 5 => 15
+# Absolute difference: | 33 - 15 | => 18
+# Example 2 Input: @ints = (1, 2, 3, 4, 5)
+# Output: 0
+# Example 3 Input: @ints = (1, 2, 34)
+# Output: 27
+#=============================================================================
+
+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 sumDiff(@ARGV);
+
+#=============================================================================
+sub sumDiff(@ints)
+{
+ my $diff = 0;
+ for my $n ( @ints )
+ {
+ $diff += $n;
+ while ( $n )
+ {
+ use integer;
+ $diff -= $n % 10;
+ $n /= 10;
+ }
+ }
+
+ return abs($diff);
+}
+
+sub runTest
+{
+ use Test2::V0;
+
+ is( sumDiff(1,23,4,5 ), 18, "Example 1");
+ is( sumDiff(1,2,3,4,5), 0, "Example 2");
+ is( sumDiff(1,2,34 ), 27, "Example 3");
+
+ done_testing;
+}
+
+sub runBenchmark($repeat)
+{
+ use Benchmark qw/cmpthese/;
+
+ cmpthese($repeat, {
+ label => sub { },
+ });
+}