diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-12 21:07:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-12 21:07:29 +0100 |
| commit | c9272fe1f84b6717791f083983b8b779e1d98981 (patch) | |
| tree | 0e92a7a47e07987d35b4d755e15c794b90cd42fd /challenge-238/spazm/python/task_1_running_sum.py | |
| parent | 159d0591c3773f7830728a0505789984ea279ed6 (diff) | |
| parent | dc6098965d5b57fca238f71bb3d8c8d2cfa408f4 (diff) | |
| download | perlweeklychallenge-club-c9272fe1f84b6717791f083983b8b779e1d98981.tar.gz perlweeklychallenge-club-c9272fe1f84b6717791f083983b8b779e1d98981.tar.bz2 perlweeklychallenge-club-c9272fe1f84b6717791f083983b8b779e1d98981.zip | |
Merge pull request #8858 from spazm/week-238-python
feature: Add TWC 238 python solution by @spazm
Diffstat (limited to 'challenge-238/spazm/python/task_1_running_sum.py')
| -rwxr-xr-x | challenge-238/spazm/python/task_1_running_sum.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/challenge-238/spazm/python/task_1_running_sum.py b/challenge-238/spazm/python/task_1_running_sum.py new file mode 100755 index 0000000000..391fb82cfa --- /dev/null +++ b/challenge-238/spazm/python/task_1_running_sum.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 + +from typing import Iterable + + +def running_sum(nums: list[int]) -> list[int]: + """ + 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) + >>> assert (1, 3, 6, 10, 15) == running_sum([1, 2, 3, 4, 5]) + Example 2 + Input: @int = (1, 1, 1, 1, 1) + Output: (1, 2, 3, 4, 5) + >>> assert (1, 2, 3, 4, 5) == running_sum([1, 1, 1, 1, 1]) + Example 3 + Input: @int = (0, -1, 1, 2) + Output: (0, -1, 0, 2) + >>> assert ( 0, -1, 0, 2) == running_sum([0, -1, 1, 2]) + """ + return list(running_sum_it(nums)) + + +def running_sum_it(nums: list[int]) -> Iterable[int]: + """ + You are given an array of integers. + + return an iterator of 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) + >>> assert (1, 3, 6, 10, 15) == list(running_sum([1, 2, 3, 4, 5])) + Example 2 + Input: @int = (1, 1, 1, 1, 1) + Output: (1, 2, 3, 4, 5) + >>> assert (1, 2, 3, 4, 5) == list(running_sum([1, 1, 1, 1, 1])) + Example 3 + Input: @int = (0, -1, 1, 2) + Output: (0, -1, 0, 2) + >>> assert ( 0, -1, 0, 2) == list(running_sum([0, -1, 1, 2])) + """ + + current_sum = 0 + for i in nums: + current_sum += i + yield current_sum + + +if __name__ == "__main__": + import unittest + + tests = [ + ([1, 2, 3, 4, 5], [1, 3, 6, 10, 15]), + ([1, 1, 1, 1, 1], [1, 2, 3, 4, 5]), + ([0, -1, 1, 2], [0, -1, 0, 2]), + ] + tester = unittest.TestCase() + for input, expected in tests: + tester.assertEqual(expected, running_sum(input)) |
