1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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])
|