aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPerlMonk-Athanasius <PerlMonk.Athanasius@gmail.com>2025-10-19 23:52:45 +1000
committerPerlMonk-Athanasius <PerlMonk.Athanasius@gmail.com>2025-10-19 23:52:45 +1000
commit8e8d251b6e854f25475ec94955a15c673c79dbe3 (patch)
treedf921b573e1a0af6a11d8dd6d4d45122579747fb
parent2e33837a5d237655e79329d8fa24e051a51d3c30 (diff)
downloadperlweeklychallenge-club-8e8d251b6e854f25475ec94955a15c673c79dbe3.tar.gz
perlweeklychallenge-club-8e8d251b6e854f25475ec94955a15c673c79dbe3.tar.bz2
perlweeklychallenge-club-8e8d251b6e854f25475ec94955a15c673c79dbe3.zip
Perl & Raku solutions to Task 1 for Week 343
-rw-r--r--challenge-343/athanasius/perl/ch-1.pl177
-rw-r--r--challenge-343/athanasius/raku/ch-1.raku167
2 files changed, 344 insertions, 0 deletions
diff --git a/challenge-343/athanasius/perl/ch-1.pl b/challenge-343/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..890afbf3d9
--- /dev/null
+++ b/challenge-343/athanasius/perl/ch-1.pl
@@ -0,0 +1,177 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 343
+=========================
+
+TASK #1
+-------
+*Zero Friend*
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given a list of numbers.
+
+Find the number that is closest to zero and return its distance to zero.
+
+Example 1
+
+ Input: @nums = (4, 2, -1, 3, -2)
+ Output: 1
+
+ Values closest to 0: -1 and 2 (distance = 1 and 2)
+
+Example 2
+
+ Input: @nums = (-5, 5, -3, 3, -1, 1)
+ Output: 1
+
+ Values closest to 0: -1 and 1 (distance = 1)
+
+Example 3
+
+ Input: @ums = (7, -3, 0, 2, -8)
+ Output: 0
+
+ Values closest to 0: 0 (distance = 0)
+ Exact zero wins regardless of other close values.
+
+Example 4
+
+ Input: @nums = (-2, -5, -1, -8)
+ Output: 1
+
+ Values closest to 0: -1 and -2 (distance = 1 and 2)
+
+Example 5
+
+ Input: @nums = (-2, 2, -4, 4, -1, 1)
+ Output: 1
+
+ Values closest to 0: -1 and 1 (distance = 1)
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2025 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=comment
+
+Assumption
+----------
+The input numbers are integers.
+
+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.38.2; # Enables strictures
+use warnings;
+use Const::Fast;
+use Regexp::Common qw( number );
+use Test::More;
+
+const my $USAGE => <<END;
+Usage:
+ perl $0 [<nums> ...]
+ perl $0
+
+ [<nums> ...] A non-empty list of integers
+END
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 343, Task #1: Zero Friend (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ my $argc = scalar @ARGV;
+
+ if (scalar @ARGV == 0)
+ {
+ run_tests();
+ }
+ else
+ {
+ my @nums = @ARGV;
+
+ / ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] )
+ for @nums;
+
+ printf "Input: \@nums = (%s)\n", join ', ', @nums;
+
+ my $min_dist = find_min_distance( \@nums );
+
+ print "Output: $min_dist\n";
+ }
+}
+
+#-------------------------------------------------------------------------------
+sub find_min_distance
+#-------------------------------------------------------------------------------
+{
+ my ($nums) = @_;
+ my @abs_nums = map { abs( $_ ) } @$nums;
+ @abs_nums = sort { $a <=> $b } @abs_nums;
+
+ return $abs_nums[0];
+}
+
+#-------------------------------------------------------------------------------
+sub run_tests
+#-------------------------------------------------------------------------------
+{
+ print "Running the test suite\n";
+
+ while (my $line = <DATA>)
+ {
+ chomp $line;
+
+ my ($test_name, $nums_str, $expected) = split / \| /x, $line;
+
+ for ($test_name, $nums_str, $expected)
+ {
+ s/ ^ \s+ //x;
+ s/ \s+ $ //x;
+ }
+
+ my @nums = split / \s+ /x, $nums_str;
+ my $min_dist = find_min_distance( \@nums );
+
+ is $min_dist, $expected, $test_name;
+ }
+
+ done_testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error
+#-------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+################################################################################
+
+__DATA__
+Example 1| 4 2 -1 3 -2 |1
+Example 2|-5 5 -3 3 -1 1|1
+Example 3| 7 -3 0 2 -8 |0
+Example 4|-2 -5 -1 -8 |1
+Example 5|-2 2 -4 4 -1 1|1
diff --git a/challenge-343/athanasius/raku/ch-1.raku b/challenge-343/athanasius/raku/ch-1.raku
new file mode 100644
index 0000000000..85e38de051
--- /dev/null
+++ b/challenge-343/athanasius/raku/ch-1.raku
@@ -0,0 +1,167 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 343
+=========================
+
+TASK #1
+-------
+*Zero Friend*
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given a list of numbers.
+
+Find the number that is closest to zero and return its distance to zero.
+
+Example 1
+
+ Input: @nums = (4, 2, -1, 3, -2)
+ Output: 1
+
+ Values closest to 0: -1 and 2 (distance = 1 and 2)
+
+Example 2
+
+ Input: @nums = (-5, 5, -3, 3, -1, 1)
+ Output: 1
+
+ Values closest to 0: -1 and 1 (distance = 1)
+
+Example 3
+
+ Input: @ums = (7, -3, 0, 2, -8)
+ Output: 0
+
+ Values closest to 0: 0 (distance = 0)
+ Exact zero wins regardless of other close values.
+
+Example 4
+
+ Input: @nums = (-2, -5, -1, -8)
+ Output: 1
+
+ Values closest to 0: -1 and -2 (distance = 1 and 2)
+
+Example 5
+
+ Input: @nums = (-2, 2, -4, 4, -1, 1)
+ Output: 1
+
+ Values closest to 0: -1 and 1 (distance = 1)
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2025 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=begin comment
+
+Assumption
+----------
+The input numbers are integers.
+
+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 element of the list is negative, it must be preceded by "--" to
+ indicated that it is not a command-line flag.
+
+=end comment
+#===============================================================================
+
+use Test;
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 343, Task #1: Zero Friend (Raku)\n".put;
+}
+
+#===============================================================================
+multi sub MAIN
+(
+ #| A non-empty list of integers
+
+ *@nums where { .elems > 0 && .all ~~ Int:D }
+)
+#===============================================================================
+{
+ "Input: \@nums = (%s)\n".printf: @nums.join: ', ';
+
+ my UInt $min-dist = find-min-distance( @nums );
+
+ "Output: $min-dist".put;
+}
+
+#===============================================================================
+multi sub MAIN() # No input: run the test suite
+#===============================================================================
+{
+ run-tests();
+}
+
+#-------------------------------------------------------------------------------
+sub find-min-distance( List:D[Int:D] $nums where { .elems > 0 } --> UInt:D )
+#-------------------------------------------------------------------------------
+{
+ return $nums.map( { .abs } ).min;
+}
+
+#-------------------------------------------------------------------------------
+sub run-tests()
+#-------------------------------------------------------------------------------
+{
+ 'Running the test suite'.put;
+
+ for test-data.lines -> Str $line
+ {
+ my Str ($test-name, $nums-str, $expected) = $line.split: / \| /;
+
+ for $test-name, $nums-str, $expected
+ {
+ s/ ^ \s+ //;
+ s/ \s+ $ //;
+ }
+
+ my Int @nums = $nums-str.split( / \s+ /, :skip-empty ).map: { .Int };
+ my UInt $dist = find-min-distance( @nums );
+
+ is $dist, $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| 4 2 -1 3 -2 |1
+ Example 2|-5 5 -3 3 -1 1|1
+ Example 3| 7 -3 0 2 -8 |0
+ Example 4|-2 -5 -1 -8 |1
+ Example 5|-2 2 -4 4 -1 1|1
+ END
+}
+
+################################################################################