aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-05-06 15:36:38 +0100
committerGitHub <noreply@github.com>2025-05-06 15:36:38 +0100
commitaeee8846c43a30b6c9ce0a368aadee695b261ecc (patch)
treec39d92e3cc7cd8cf3f5392c9b4461d91bb47ef21
parentf04f7ceff2f3b2ebcea48cde0f59b4191050526f (diff)
parent57b52fca047e65f064f913876a2dee4133766d85 (diff)
downloadperlweeklychallenge-club-aeee8846c43a30b6c9ce0a368aadee695b261ecc.tar.gz
perlweeklychallenge-club-aeee8846c43a30b6c9ce0a368aadee695b261ecc.tar.bz2
perlweeklychallenge-club-aeee8846c43a30b6c9ce0a368aadee695b261ecc.zip
Merge pull request #11985 from PerlMonk-Athanasius/branch-for-challenge-320
Perl & Raku solutions to Tasks 1 & 2 for Week 320
-rw-r--r--challenge-320/athanasius/perl/ch-1.pl172
-rw-r--r--challenge-320/athanasius/perl/ch-2.pl176
-rw-r--r--challenge-320/athanasius/raku/ch-1.raku165
-rw-r--r--challenge-320/athanasius/raku/ch-2.raku168
4 files changed, 681 insertions, 0 deletions
diff --git a/challenge-320/athanasius/perl/ch-1.pl b/challenge-320/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..794c0ec3eb
--- /dev/null
+++ b/challenge-320/athanasius/perl/ch-1.pl
@@ -0,0 +1,172 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 320
+=========================
+
+TASK #1
+-------
+*Maximum Count*
+
+Submitted by: Mohammad Sajid Anwar
+
+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
+
+ There are 3 positive integers.
+ There are 3 negative integers.
+ The maximum between 3 and 3 is 3.
+
+Example 2
+
+ Input: @ints = (-2, -1, 0, 0, 1)
+ Output: 2
+
+ There are 1 positive integers.
+ There are 2 negative integers.
+ The maximum between 2 and 1 is 2.
+
+Example 3
+
+ Input: @ints = (1, 2, 3, 4)
+ Output: 4
+
+ There are 4 positive integers.
+ There are 0 negative integers.
+ The maximum between 4 and 0 is 4.
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2025 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=comment
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. A non-empty list of integers is entered on the command-line.
+
+=cut
+#===============================================================================
+
+use v5.32; # Enables strictures
+use warnings;
+use Const::Fast;
+use Devel::Assert qw( on );
+use Regexp::Common qw( number );
+use Test::More;
+
+const my $USAGE => <<END;
+Usage:
+ perl $0 [<ints> ...]
+ perl $0
+
+ [<ints> ...] A non-empty list of integers
+END
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 320, Task #1: Maximum Count (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ if (scalar @ARGV == 0)
+ {
+ run_tests();
+ }
+ else
+ {
+ / ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] )
+ for @ARGV;
+
+ my @ints = @ARGV;
+
+ printf "Input: \@ints = (%s)\n", join ', ', @ints;
+
+ my $max_count = find_max_count( \@ints );
+
+ print "Output: $max_count\n";
+ }
+}
+
+#-------------------------------------------------------------------------------
+sub find_max_count
+#-------------------------------------------------------------------------------
+{
+ my ($ints) = @_;
+ my $pos = 0;
+ my $nil = 0;
+ my $neg = 0;
+
+ for my $int (@$ints)
+ {
+ my $target = $int > 0 ? \$pos : $int == 0 ? \$nil : \$neg;
+ ++$$target;
+ }
+
+ assert $pos + $nil + $neg == scalar @$ints; # Sanity check
+
+ return $pos >= $neg ? $pos : $neg;
+}
+
+#-------------------------------------------------------------------------------
+sub run_tests
+#-------------------------------------------------------------------------------
+{
+ print "Running the test suite\n";
+
+ while (my $line = <DATA>)
+ {
+ chomp $line;
+
+ my ($test_name, $int_str, $expected) = split / \| /x, $line;
+
+ for ($test_name, $int_str, $expected)
+ {
+ s/ ^ \s+ //x;
+ s/ \s+ $ //x;
+ }
+
+ my @ints = split / \s+ /x, $int_str;
+ my $max_count = find_max_count( \@ints );
+
+ is $max_count, $expected, $test_name;
+ }
+
+ done_testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error
+#-------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+################################################################################
+
+__DATA__
+Example 1|-3 -2 -1 1 2 3|3
+Example 2|-2 -1 0 0 1 |2
+Example 3| 1 2 3 4 |4
diff --git a/challenge-320/athanasius/perl/ch-2.pl b/challenge-320/athanasius/perl/ch-2.pl
new file mode 100644
index 0000000000..84b96e6ea9
--- /dev/null
+++ b/challenge-320/athanasius/perl/ch-2.pl
@@ -0,0 +1,176 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 320
+=========================
+
+TASK #2
+-------
+*Sum Difference*
+
+Submitted by: Mohammad Sajid Anwar
+
+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
+
+ Element sum: 1 + 2 + 3 + 4 + 5 => 15
+ Digit sum: 1 + 2 + 3 + 4 + 5 => 15
+ Absolute difference: | 15 - 15 | => 0
+
+Example 3
+
+ Input: @ints = (1, 2, 34)
+ Output: 27
+
+ Element sum: 1 + 2 + 34 => 37
+ Digit sum: 1 + 2 + 3 + 4 => 10
+ Absolute difference: | 37 - 10 | => 27
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2025 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=comment
+
+Assumption
+----------
+A "positive" integer is greater than or equal to zero (i.e., unsigned).
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. A non-empty list of unsigned integers is entered on the command-line.
+
+=cut
+#===============================================================================
+
+use v5.32; # Enables strictures
+use warnings;
+use Const::Fast;
+use Regexp::Common qw( number );
+use Test::More;
+
+const my $USAGE => <<END;
+Usage:
+ perl $0 [<ints> ...]
+ perl $0
+
+ [<ints> ...] A non-empty list of unsigned integers
+END
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 320, Task #2: Sum Difference (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ if (scalar @ARGV == 0)
+ {
+ run_tests();
+ }
+ else
+ {
+ for (@ARGV)
+ {
+ / ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] );
+ $_ >= 0 or error( "$_ is not a positive integer" );
+ }
+
+ my @ints = map { $_ + 0 } @ARGV; # Normalize (remove initial "+")
+
+ printf "Input: \@ints = (%s)\n", join ', ', @ints;
+
+ my $sum_diff = find_sum_diff( \@ints );
+
+ print "Output: $sum_diff\n";
+ }
+}
+
+#-------------------------------------------------------------------------------
+sub find_sum_diff
+#-------------------------------------------------------------------------------
+{
+ my ($ints) = @_;
+ my $element_sum = 0;
+ my $digit_sum = 0;
+
+ for my $int (@$ints)
+ {
+ $element_sum += $int;
+ $digit_sum += $_ for split //, $int;
+ }
+
+ return abs( $element_sum - $digit_sum );
+}
+
+#-------------------------------------------------------------------------------
+sub run_tests
+#-------------------------------------------------------------------------------
+{
+ print "Running the test suite\n";
+
+ while (my $line = <DATA>)
+ {
+ chomp $line;
+
+ my ($test_name, $int_str, $expected) = split / \| /x, $line;
+
+ for ($test_name, $int_str, $expected)
+ {
+ s/ ^ \s+ //x;
+ s/ \s+ $ //x;
+ }
+
+ my @ints = split / \s+ /x, $int_str;
+ my $sum_diff = find_sum_diff( \@ints );
+
+ is $sum_diff, $expected, $test_name;
+ }
+
+ done_testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error
+#-------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+################################################################################
+
+__DATA__
+Example 1|1 23 4 5 |18
+Example 2|1 2 3 4 5| 0
+Example 3|1 2 34 |27
+Include 0|1 23 4 0 5|18
diff --git a/challenge-320/athanasius/raku/ch-1.raku b/challenge-320/athanasius/raku/ch-1.raku
new file mode 100644
index 0000000000..8c359d3436
--- /dev/null
+++ b/challenge-320/athanasius/raku/ch-1.raku
@@ -0,0 +1,165 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 320
+=========================
+
+TASK #1
+-------
+*Maximum Count*
+
+Submitted by: Mohammad Sajid Anwar
+
+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
+
+ There are 3 positive integers.
+ There are 3 negative integers.
+ The maximum between 3 and 3 is 3.
+
+Example 2
+
+ Input: @ints = (-2, -1, 0, 0, 1)
+ Output: 2
+
+ There are 1 positive integers.
+ There are 2 negative integers.
+ The maximum between 2 and 1 is 2.
+
+Example 3
+
+ Input: @ints = (1, 2, 3, 4)
+ Output: 4
+
+ There are 4 positive integers.
+ There are 0 negative integers.
+ The maximum between 4 and 0 is 4.
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2025 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=begin comment
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. A non-empty list of integers is entered on the command-line.
+3. If the first list element is negative, it must preceded by "--" to indicate
+ that it is not a command-line flag.
+
+=end comment
+#===============================================================================
+
+use Test;
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 320, Task #1: Maximum Count (Raku)\n".put;
+}
+
+#===============================================================================
+multi sub MAIN
+(
+ *@ints where { .elems > 0 && .all ~~ Int:D } #= A non-empty list of integers
+)
+#===============================================================================
+{
+ "Input: \@ints = (%s)\n".printf: @ints.join: ', ';
+
+ my UInt $max-count = find-max-count( @ints );
+
+ "Output: $max-count".put;
+}
+
+#===============================================================================
+multi sub MAIN() # No input: run the test suite
+#===============================================================================
+{
+ run-tests();
+}
+
+#-------------------------------------------------------------------------------
+sub find-max-count( List:D[Int:D] $ints --> UInt:D )
+#-------------------------------------------------------------------------------
+{
+ my UInt $pos = 0;
+ my UInt $nil = 0;
+ my UInt $neg = 0;
+
+ for @$ints -> Int $int
+ {
+ my UInt $target := $int > 0 ?? $pos !! $int == 0 ?? $nil !! $neg;
+
+ ++$target;
+ }
+
+ $pos + $nil + $neg == $ints.elems or die 'Logic error'; # Sanity check
+
+ return ($pos, $neg).max;
+}
+
+#-------------------------------------------------------------------------------
+sub run-tests()
+#-------------------------------------------------------------------------------
+{
+ 'Running the test suite'.put;
+
+ for test-data.lines -> Str $line
+ {
+ my Str ($test-name, $int-str, $expected) = $line.split: / \| /;
+
+ for $test-name, $int-str, $expected
+ {
+ s/ ^ \s+ //;
+ s/ \s+ $ //;
+ }
+
+ my Int @ints = $int-str.split( / \s+ /, :skip-empty )
+ .map: { .Int };
+ my UInt $max-count = find-max-count( @ints );
+
+ is $max-count, $expected.Int, $test-name;
+ }
+
+ done-testing;
+}
+
+#-------------------------------------------------------------------------------
+sub USAGE()
+#-------------------------------------------------------------------------------
+{
+ my Str $usage = $*USAGE;
+
+ $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;
+
+ $usage.put;
+}
+
+#-------------------------------------------------------------------------------
+sub test-data( --> Str:D )
+#-------------------------------------------------------------------------------
+{
+ return q:to/END/;
+ Example 1|-3 -2 -1 1 2 3|3
+ Example 2|-2 -1 0 0 1 |2
+ Example 3| 1 2 3 4 |4
+ END
+}
+
+################################################################################
diff --git a/challenge-320/athanasius/raku/ch-2.raku b/challenge-320/athanasius/raku/ch-2.raku
new file mode 100644
index 0000000000..611d4f43b1
--- /dev/null
+++ b/challenge-320/athanasius/raku/ch-2.raku
@@ -0,0 +1,168 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 320
+=========================
+
+TASK #2
+-------
+*Sum Difference*
+
+Submitted by: Mohammad Sajid Anwar
+
+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
+
+ Element sum: 1 + 2 + 3 + 4 + 5 => 15
+ Digit sum: 1 + 2 + 3 + 4 + 5 => 15
+ Absolute difference: | 15 - 15 | => 0
+
+Example 3
+
+ Input: @ints = (1, 2, 34)
+ Output: 27
+
+ Element sum: 1 + 2 + 34 => 37
+ Digit sum: 1 + 2 + 3 + 4 => 10
+ Absolute difference: | 37 - 10 | => 27
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2025 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=begin comment
+
+Assumption
+----------
+A "positive" integer is greater than or equal to zero (i.e., unsigned).
+
+Interface
+---------
+1. If no command-line arguments are given, the test suite is run. Otherwise:
+2. A non-empty list of unsigned integers is entered on the command-line.
+
+=end comment
+#===============================================================================
+
+use Test;
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 320, Task #2: Sum Difference (Raku)\n".put;
+}
+
+#===============================================================================
+multi sub MAIN
+(
+ #| A non-empty list of unsigned integers
+
+ *@ints where { .elems > 0 && .all ~~ UInt:D }
+)
+#===============================================================================
+{
+ @ints .= map: { $_ + 0 }; # Normalize (remove initial "+")
+
+ "Input: \@ints = (%s)\n".printf: @ints.join: ', ';
+
+ my UInt $sum-diff = find-sum-diff( @ints );
+
+ "Output: $sum-diff".put;
+}
+
+#===============================================================================
+multi sub MAIN() # No input: run the test suite
+#===============================================================================
+{
+ run-tests();
+}
+
+#-------------------------------------------------------------------------------
+sub find-sum-diff( List:D[UInt:D] $ints --> UInt:D )
+#-------------------------------------------------------------------------------
+{
+ my UInt $element-sum = 0;
+ my UInt $digit-sum = 0;
+
+ for @$ints -> UInt $int
+ {
+ $element-sum += $int;
+ $digit-sum += $_ for $int.split: '', :skip-empty;
+ }
+
+ return ($element-sum - $digit-sum).abs;
+}
+
+#-------------------------------------------------------------------------------
+sub run-tests()
+#-------------------------------------------------------------------------------
+{
+ 'Running the test suite'.put;
+
+ for test-data.lines -> Str $line
+ {
+ my Str ($test-name, $int-str, $expected) = $line.split: / \| /;
+
+ for $test-name, $int-str, $expected
+ {
+ s/ ^ \s+ //;
+ s/ \s+ $ //;
+ }
+
+ my UInt @ints = $int-str.split( / \s+ /, :skip-empty )
+ .map: { .Int };
+ my UInt $sum-diff = find-sum-diff( @ints );
+
+ is $sum-diff, $expected.Int, $test-name;
+ }
+
+ done-testing;
+}
+
+#-------------------------------------------------------------------------------
+sub USAGE()
+#-------------------------------------------------------------------------------
+{
+ my Str $usage = $*USAGE;
+
+ $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/;
+
+ $usage.put;
+}
+
+#-------------------------------------------------------------------------------
+sub test-data( --> Str:D )
+#-------------------------------------------------------------------------------
+{
+ return q:to/END/;
+ Example 1|1 23 4 5 |18
+ Example 2|1 2 3 4 5| 0
+ Example 3|1 2 34 |27
+ Include 0|1 23 4 0 5|18
+ END
+}
+
+################################################################################