aboutsummaryrefslogtreecommitdiff
path: root/challenge-238/jeanluc2020/python/ch-1.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-12 20:57:24 +0100
committerGitHub <noreply@github.com>2023-10-12 20:57:24 +0100
commit7cf48fb57571e15ee47cd112fcde92872327dbd2 (patch)
treed84e42464646f30a530a677c6228e4682d4ac599 /challenge-238/jeanluc2020/python/ch-1.py
parent3143f9657ea324e7588d575d67c35eb28bc276f3 (diff)
parent7fd4d9df83269a9de352d8a9b30eff594f92b483 (diff)
downloadperlweeklychallenge-club-7cf48fb57571e15ee47cd112fcde92872327dbd2.tar.gz
perlweeklychallenge-club-7cf48fb57571e15ee47cd112fcde92872327dbd2.tar.bz2
perlweeklychallenge-club-7cf48fb57571e15ee47cd112fcde92872327dbd2.zip
Merge pull request #8856 from jeanluc2020/jeanluc-238
Add solution 238.
Diffstat (limited to 'challenge-238/jeanluc2020/python/ch-1.py')
-rwxr-xr-xchallenge-238/jeanluc2020/python/ch-1.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/challenge-238/jeanluc2020/python/ch-1.py b/challenge-238/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..de9447fd3e
--- /dev/null
+++ b/challenge-238/jeanluc2020/python/ch-1.py
@@ -0,0 +1,49 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/#TASK1
+#
+# Task 1: Running Sum
+# ===================
+#
+# You are given an array of integers.
+#
+# Write a script to return the running sum of the given array. The running sum
+# can be calculated as sum[i] = num[0] + num[1] + …. + num[i].
+#
+## Example 1
+##
+## Input: @int = (1, 2, 3, 4, 5)
+## Output: (1, 3, 6, 10, 15)
+#
+## Example 2
+##
+## Input: @int = (1, 1, 1, 1, 1)
+## Output: (1, 2, 3, 4, 5)
+#
+## Example 3
+##
+## Input: @int = (0, -1, 1, 2)
+## Output: (0, -1, 0, 2)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# This one is straight forward: For each element, add it to
+# the current sum and add the sum to the result array.
+
+
+def running_sum(ints: list):
+ sum = 0
+ result = []
+ print("Input: (", ", ".join(str(x) for x in ints), ")")
+ for elem in ints:
+ sum += elem
+ result.append(sum)
+ print("Output: (", ", ".join(str(x) for x in result), ")")
+
+
+running_sum([1, 2, 3, 4, 5])
+running_sum([1, 1, 1, 1, 1])
+running_sum([0, -1, 1, 2])