aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2025-09-19 12:42:27 +0200
committerMariano Spadaccini <spadacciniweb@gmail.com>2025-09-19 12:42:27 +0200
commitff71ca31aa9561aeae6d9a8338db6de63f93066b (patch)
treee5fb36191b1025f86e7d2ce498732f5e011f29dc
parent1bbbc56e6a0dc51d8d37d8e7d9c2b24b3dfb3294 (diff)
downloadperlweeklychallenge-club-ff71ca31aa9561aeae6d9a8338db6de63f93066b.tar.gz
perlweeklychallenge-club-ff71ca31aa9561aeae6d9a8338db6de63f93066b.tar.bz2
perlweeklychallenge-club-ff71ca31aa9561aeae6d9a8338db6de63f93066b.zip
PWC 339: add ch-2 in Elixir
-rw-r--r--challenge-339/spadacciniweb/elixir/ch-2.exs102
1 files changed, 102 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)