aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-21 13:50:17 +0100
committerGitHub <noreply@github.com>2025-09-21 13:50:17 +0100
commit7e37a38d692ffe6fa52ddda00af248f5add0688f (patch)
tree73844d133b5ede7cc8a7304bc148a59e8b462fa7
parent90acec84c5d60ec80b2bbe1eccb0ba5dfe09caa5 (diff)
parent7c27b18bcd86a2e043d3f24e99fc0155d9184c66 (diff)
downloadperlweeklychallenge-club-7e37a38d692ffe6fa52ddda00af248f5add0688f.tar.gz
perlweeklychallenge-club-7e37a38d692ffe6fa52ddda00af248f5add0688f.tar.bz2
perlweeklychallenge-club-7e37a38d692ffe6fa52ddda00af248f5add0688f.zip
Merge pull request #12708 from PerlMonk-Athanasius/branch-for-challenge-339
Perl and Raku solutions to Task 2 for Week 339
-rw-r--r--challenge-339/athanasius/perl/ch-2.pl215
-rw-r--r--challenge-339/athanasius/raku/ch-2.raku207
2 files changed, 422 insertions, 0 deletions
diff --git a/challenge-339/athanasius/perl/ch-2.pl b/challenge-339/athanasius/perl/ch-2.pl
new file mode 100644
index 0000000000..9f65629f1f
--- /dev/null
+++ b/challenge-339/athanasius/perl/ch-2.pl
@@ -0,0 +1,215 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 339
+=========================
+
+TASK #2
+-------
+*Peak Point*
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of altitude gain.
+
+Write a script to find the peak point gained.
+
+Example 1
+
+ Input: @gain = (-5, 1, 5, -9, 2)
+ Output: 1
+
+ start: 0
+ 1st change: 0 + (-5) = -5
+ 2nd change: -5 + 1 = -4
+ 3rd change: -4 + 5 = 1
+ 4th change: 1 + (-9) = -8
+ 5th change: -8 + 2 = -6
+
+ max(0, -5, -4, 1, -8, -6) = 1
+
+Example 2
+
+ Input: @gain = (10, 10, 10, -25)
+ Output: 30
+
+ start: 0
+ 1st change: 0 + 10 = 10
+ 2nd change: 10 + 10 = 20
+ 3rd change: 20 + 10 = 30
+ 4th change: 30 + (-25) = 5
+
+ max(0, 10, 20, 30, 5) = 30
+
+Example 3
+
+ Input: @gain = (3, -4, 2, 5, -6, 1)
+ Output: 6
+
+ start: 0
+ 1st change: 0 + 3 = 3
+ 2nd change: 3 + (-4) = -1
+ 3rd change: -1 + 2 = 1
+ 4th change: 1 + 5 = 6
+ 5th change: 6 + (-6) = 0
+ 6th change: 0 + 1 = 1
+
+ max(0, 3, -1, 1, 6, 0, 1) = 6
+
+Example 4
+
+ Input: @gain = (-1, -2, -3, -4)
+ Output: 0
+
+ start: 0
+ 1st change: 0 + (-1) = -1
+ 2nd change: -1 + (-2) = -3
+ 3rd change: -3 + (-3) = -6
+ 4th change: -6 + (-4) = -10
+
+ max(0, -1, -3, -6, -10) = 0
+
+Example 5
+
+ Input: @gain = (-10, 15, 5)
+ Output: 10
+
+ start: 0
+ 1st change: 0 + (-10) = -10
+ 2nd change: -10 + 15 = 5
+ 3rd change: 5 + 5 = 10
+
+ max(0, -10, 5, 10) = 10
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2025 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=comment
+
+Assumption
+----------
+Altitude gains 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 [<gain> ...]
+ perl $0
+
+ [<gain> ...] A non-empty list of altitude gains (integers)
+END
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ $| = 1;
+ print "\nChallenge 339, Task #2: Peak Point (Perl)\n\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ if (scalar @ARGV == 0)
+ {
+ run_tests();
+ }
+ else
+ {
+ my @gain = @ARGV;
+
+ for (@gain)
+ {
+ / ^ $RE{num}{int} $ /x or error( qq["$_" is not a valid integer] );
+ }
+
+ printf "Input: \@gain = (%s)\n", join ', ', @gain;
+
+ my $peak = find_peak_point( \@gain );
+
+ print "Output: $peak\n";
+ }
+}
+
+#-------------------------------------------------------------------------------
+sub find_peak_point
+#-------------------------------------------------------------------------------
+{
+ my ($gains) = @_;
+ my $peak = 0;
+ my $altitude = 0; # Start
+
+ for my $gain (@$gains)
+ {
+ $altitude += $gain;
+ $peak = $altitude if $altitude > $peak;
+ }
+
+ return $peak;
+}
+
+#-------------------------------------------------------------------------------
+sub run_tests
+#-------------------------------------------------------------------------------
+{
+ print "Running the test suite\n";
+
+ while (my $line = <DATA>)
+ {
+ chomp $line;
+
+ my ($test_name, $gains_str, $expected) = split / \| /x, $line;
+
+ for ($test_name, $gains_str, $expected)
+ {
+ s/ ^ \s+ //x;
+ s/ \s+ $ //x;
+ }
+
+ my @gain = split / \s+ /x, $gains_str;
+ my $peak = find_peak_point( \@gain );
+
+ is $peak, $expected, $test_name;
+ }
+
+ done_testing;
+}
+
+#-------------------------------------------------------------------------------
+sub error
+#-------------------------------------------------------------------------------
+{
+ my ($message) = @_;
+
+ die "ERROR: $message\n$USAGE";
+}
+
+################################################################################
+
+__DATA__
+Example 1| -5 1 5 -9 2 | 1
+Example 2| 10 10 10 -25 |30
+Example 3| 3 -4 2 5 -6 1| 6
+Example 4| -1 -2 -3 -4 | 0
+Example 5|-10 15 5 |10
diff --git a/challenge-339/athanasius/raku/ch-2.raku b/challenge-339/athanasius/raku/ch-2.raku
new file mode 100644
index 0000000000..c9ede0a9de
--- /dev/null
+++ b/challenge-339/athanasius/raku/ch-2.raku
@@ -0,0 +1,207 @@
+use v6d;
+
+################################################################################
+=begin comment
+
+Perl Weekly Challenge 339
+=========================
+
+TASK #2
+-------
+*Peak Point*
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of altitude gain.
+
+Write a script to find the peak point gained.
+
+Example 1
+
+ Input: @gain = (-5, 1, 5, -9, 2)
+ Output: 1
+
+ start: 0
+ 1st change: 0 + (-5) = -5
+ 2nd change: -5 + 1 = -4
+ 3rd change: -4 + 5 = 1
+ 4th change: 1 + (-9) = -8
+ 5th change: -8 + 2 = -6
+
+ max(0, -5, -4, 1, -8, -6) = 1
+
+Example 2
+
+ Input: @gain = (10, 10, 10, -25)
+ Output: 30
+
+ start: 0
+ 1st change: 0 + 10 = 10
+ 2nd change: 10 + 10 = 20
+ 3rd change: 20 + 10 = 30
+ 4th change: 30 + (-25) = 5
+
+ max(0, 10, 20, 30, 5) = 30
+
+Example 3
+
+ Input: @gain = (3, -4, 2, 5, -6, 1)
+ Output: 6
+
+ start: 0
+ 1st change: 0 + 3 = 3
+ 2nd change: 3 + (-4) = -1
+ 3rd change: -1 + 2 = 1
+ 4th change: 1 + 5 = 6
+ 5th change: 6 + (-6) = 0
+ 6th change: 0 + 1 = 1
+
+ max(0, 3, -1, 1, 6, 0, 1) = 6
+
+Example 4
+
+ Input: @gain = (-1, -2, -3, -4)
+ Output: 0
+
+ start: 0
+ 1st change: 0 + (-1) = -1
+ 2nd change: -1 + (-2) = -3
+ 3rd change: -3 + (-3) = -6
+ 4th change: -6 + (-4) = -10
+
+ max(0, -1, -3, -6, -10) = 0
+
+Example 5
+
+ Input: @gain = (-10, 15, 5)
+ Output: 10
+
+ start: 0
+ 1st change: 0 + (-10) = -10
+ 2nd change: -10 + 15 = 5
+ 3rd change: 5 + 5 = 10
+
+ max(0, -10, 5, 10) = 10
+
+=end comment
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2025 PerlMonk Athanasius #
+#--------------------------------------#
+
+#===============================================================================
+=begin comment
+
+Assumption
+----------
+Altitude gains 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 integer is negative, it must be preceded by "--" to indicate
+ that it is not a command-line flag.
+
+=end comment
+#===============================================================================
+
+use Test;
+
+#-------------------------------------------------------------------------------
+BEGIN
+#-------------------------------------------------------------------------------
+{
+ "\nChallenge 339, Task #2: Peak Point (Raku)\n".put;
+}
+
+#===============================================================================
+multi sub MAIN
+(
+ #| A non-empty list of altitude gains (integers)
+
+ *@gain where { .elems > 0 && .all ~~ Int:D }
+)
+#===============================================================================
+{
+ "Input: \@gain = (%s)\n".printf: @gain.join: ', ';
+
+ my Int $peak = find-peak-point( @gain );
+
+ "Output: $peak".put;
+}
+
+#===============================================================================
+multi sub MAIN() # No input: run the test suite
+#===============================================================================
+{
+ run-tests();
+}
+
+#-------------------------------------------------------------------------------
+sub find-peak-point( List:D[Int:D] $gains --> Int:D )
+#-------------------------------------------------------------------------------
+{
+ my Int $peak = 0;
+ my Int $altitude = 0; # Start
+
+ for @$gains -> Int $gain
+ {
+ $altitude += $gain;
+ $peak = $altitude if $altitude > $peak;
+ }
+
+ return $peak;
+}
+
+#-------------------------------------------------------------------------------
+sub run-tests()
+#-------------------------------------------------------------------------------
+{
+ 'Running the test suite'.put;
+
+ for test-data.lines -> Str $line
+ {
+ my Str ($test-name, $gains-str, $expected) = $line.split: / \| /;
+
+ for $test-name, $gains-str, $expected
+ {
+ s/ ^ \s+ //;
+ s/ \s+ $ //;
+ }
+
+ my Int @gain = $gains-str.split( / \s+ /, :skip-empty ).map: { .Int };
+ my Int $peak = find-peak-point( @gain );
+
+ is $peak, $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| -5 1 5 -9 2 | 1
+ Example 2| 10 10 10 -25 |30
+ Example 3| 3 -4 2 5 -6 1| 6
+ Example 4| -1 -2 -3 -4 | 0
+ Example 5|-10 15 5 |10
+ END
+}
+
+################################################################################