diff options
Diffstat (limited to 'challenge-238/jeanluc2020/python/ch-1.py')
| -rwxr-xr-x | challenge-238/jeanluc2020/python/ch-1.py | 49 |
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]) |
