aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-19 17:55:14 +0100
committerGitHub <noreply@github.com>2025-09-19 17:55:14 +0100
commite2ee77329c46e63c753e48ec2b0aca36fcbadebf (patch)
tree923008fa8f110919a93d5a37ece3fc59e5fcfaff
parent8e5f99084f117dd63a6578ba1f15fc4e64dd8f13 (diff)
parent819746a40637d08b83e4e04d38d2cfd2972a7712 (diff)
downloadperlweeklychallenge-club-e2ee77329c46e63c753e48ec2b0aca36fcbadebf.tar.gz
perlweeklychallenge-club-e2ee77329c46e63c753e48ec2b0aca36fcbadebf.tar.bz2
perlweeklychallenge-club-e2ee77329c46e63c753e48ec2b0aca36fcbadebf.zip
Merge pull request #12699 from spadacciniweb/PWC-339
PWC 339 - ch-2
-rw-r--r--challenge-339/spadacciniweb/elixir/ch-2.exs102
-rw-r--r--challenge-339/spadacciniweb/go/ch-2.go103
-rw-r--r--challenge-339/spadacciniweb/perl/ch-2.pl100
-rw-r--r--challenge-339/spadacciniweb/python/ch-2.py88
-rw-r--r--challenge-339/spadacciniweb/ruby/ch-2.rb91
5 files changed, 484 insertions, 0 deletions
diff --git a/challenge-339/spadacciniweb/elixir/ch-2.exs b/challenge-339/spadacciniweb/elixir/ch-2.exs
new file mode 100644
index 0000000000..9d453cc2b4
--- /dev/null
+++ b/challenge-339/spadacciniweb/elixir/ch-2.exs
@@ -0,0 +1,102 @@
+# 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
+
+defmodule Peak do
+
+ def get_gains([head | tail], acc) do
+ [acc | get_gains(tail, head + acc) ]
+ end
+
+ def get_gains([], acc) do
+ [acc]
+ end
+
+ def get_peak(gains) do
+ Enum.max( get_gains(gains, 0) )
+ end
+
+ def out(gains) do
+ IO.write( "(" <> Enum.join(gains, ",") <> ") -> ")
+ IO.puts( get_peak(gains) )
+ end
+
+end
+
+gains = [-5, 1, 5, -9, 2]
+Peak.out(gains)
+
+gains = [10, 10, 10, -25]
+Peak.out(gains)
+
+gains = [3, -4, 2, 5, -6, 1]
+Peak.out(gains)
+
+gains = [-1, -2, -3, -4]
+Peak.out(gains)
+
+gains = [-10, 15, 5]
+Peak.out(gains)
diff --git a/challenge-339/spadacciniweb/go/ch-2.go b/challenge-339/spadacciniweb/go/ch-2.go
new file mode 100644
index 0000000000..1c99d23bf0
--- /dev/null
+++ b/challenge-339/spadacciniweb/go/ch-2.go
@@ -0,0 +1,103 @@
+/*
+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
+*/
+
+package main
+
+import (
+ "fmt"
+)
+
+func get_max_peak(gain []int) {
+ peak := 0
+ max_peak := peak
+ for _, v := range gain {
+ peak = peak + v
+ if (peak > max_peak) {
+ max_peak = peak
+ }
+ }
+ fmt.Printf("gains %v -> peak %d\n", gain, max_peak)
+}
+
+func main() {
+ gain := []int{-5, 1, 5, -9, 2}
+ get_max_peak(gain)
+
+ gain = []int{10, 10, 10, -25}
+ get_max_peak(gain)
+
+ gain = []int{3, -4, 2, 5, -6, 1}
+ get_max_peak(gain)
+
+ gain = []int{-1, -2, -3, -4}
+ get_max_peak(gain)
+
+ gain = []int{-10, 15, 5}
+ get_max_peak(gain)
+}
diff --git a/challenge-339/spadacciniweb/perl/ch-2.pl b/challenge-339/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..1171088ad2
--- /dev/null
+++ b/challenge-339/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,100 @@
+#!/usr/bin/env perl
+
+# 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
+
+use strict;
+use warnings;
+use List::Util qw(max);
+
+my @gain = (-5, 1, 5, -9, 2);
+get_max_peak(\@gain);
+
+@gain = (10, 10, 10, -25);
+get_max_peak(\@gain);
+
+@gain = (3, -4, 2, 5, -6, 1);
+get_max_peak(\@gain);
+
+@gain = (-1, -2, -3, -4);
+get_max_peak(\@gain);
+
+@gain = (-10, 15, 5);
+get_max_peak(\@gain);
+
+exit 0;
+
+sub get_max_peak {
+ my $array = shift;
+
+ my @peak = (0);
+ foreach my $step (@$array) {
+ push @peak, $peak[-1] + $step;
+ }
+ printf "(%s) -> %d\n", (join ', ', @$array),
+ max @peak;
+}
diff --git a/challenge-339/spadacciniweb/python/ch-2.py b/challenge-339/spadacciniweb/python/ch-2.py
new file mode 100644
index 0000000000..898d2be41e
--- /dev/null
+++ b/challenge-339/spadacciniweb/python/ch-2.py
@@ -0,0 +1,88 @@
+# 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
+
+def get_max_peak(gain):
+ peak = [0]
+ for step in gain:
+ peak.append( peak[-1] + step )
+ print("(%s) -> %d" % ( " ".join(map("{}".format, gain )), max(peak)) )
+
+if __name__ == "__main__":
+ gain = (-5, 1, 5, -9, 2)
+ get_max_peak(gain)
+
+ gain = (10, 10, 10, -25)
+ get_max_peak(gain)
+
+ gain = (3, -4, 2, 5, -6, 1)
+ get_max_peak(gain)
+
+ gain = (-1, -2, -3, -4)
+ get_max_peak(gain)
+
+ gain = (-10, 15, 5)
+ get_max_peak(gain)
diff --git a/challenge-339/spadacciniweb/ruby/ch-2.rb b/challenge-339/spadacciniweb/ruby/ch-2.rb
new file mode 100644
index 0000000000..ace295529a
--- /dev/null
+++ b/challenge-339/spadacciniweb/ruby/ch-2.rb
@@ -0,0 +1,91 @@
+# 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
+
+def get_max_peak(gains)
+ peaks = [0]
+ gains.each { |x|
+ peaks.push peaks[-1] + x
+ }
+# }
+ printf "(%s) -> %d\n", gains.join(","),
+ peaks.max
+end
+
+gains = [-5, 1, 5, -9, 2]
+get_max_peak(gains)
+
+gains = [10, 10, 10, -25]
+get_max_peak(gains)
+
+gains = [3, -4, 2, 5, -6, 1]
+get_max_peak(gains)
+
+gains = [-1, -2, -3, -4]
+get_max_peak(gains)
+
+gains = [-10, 15, 5]
+get_max_peak(gains)