|
| 1 | +""" |
| 2 | +Calculate the Product Sum from a Special Array. |
| 3 | +reference: https://dev.to/sfrasica/algorithms-product-sum-from-an-array-dc6 |
| 4 | +
|
| 5 | +Python doctests can be run with the following command: |
| 6 | +python -m doctest -v product_sum.py |
| 7 | +
|
| 8 | +Calculate the product sum of a "special" array which can contain integers or nested |
| 9 | +arrays. The product sum is obtained by adding all elements and multiplying by their |
| 10 | +respective depths. |
| 11 | +
|
| 12 | +For example, in the array [x, y], the product sum is (x + y). In the array [x, [y, z]], |
| 13 | +the product sum is x + 2 * (y + z). In the array [x, [y, [z]]], |
| 14 | +the product sum is x + 2 * (y + 3z). |
| 15 | +
|
| 16 | +Example Input: |
| 17 | +[5, 2, [-7, 1], 3, [6, [-13, 8], 4]] |
| 18 | +Output: 12 |
| 19 | +
|
| 20 | +""" |
| 21 | + |
| 22 | + |
| 23 | +def product_sum(arr: list[int | list], depth: int) -> int: |
| 24 | + """ |
| 25 | + Recursively calculates the product sum of an array. |
| 26 | +
|
| 27 | + The product sum of an array is defined as the sum of its elements multiplied by |
| 28 | + their respective depths. If an element is a list, its product sum is calculated |
| 29 | + recursively by multiplying the sum of its elements with its depth plus one. |
| 30 | +
|
| 31 | + Args: |
| 32 | + arr: The array of integers and nested lists. |
| 33 | + depth: The current depth level. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + int: The product sum of the array. |
| 37 | +
|
| 38 | + Examples: |
| 39 | + >>> product_sum([1, 2, 3], 1) |
| 40 | + 6 |
| 41 | + >>> product_sum([-1, 2, [-3, 4]], 2) |
| 42 | + 8 |
| 43 | + >>> product_sum([1, 2, 3], -1) |
| 44 | + -6 |
| 45 | + >>> product_sum([1, 2, 3], 0) |
| 46 | + 0 |
| 47 | + >>> product_sum([1, 2, 3], 7) |
| 48 | + 42 |
| 49 | + >>> product_sum((1, 2, 3), 7) |
| 50 | + 42 |
| 51 | + >>> product_sum({1, 2, 3}, 7) |
| 52 | + 42 |
| 53 | + >>> product_sum([1, -1], 1) |
| 54 | + 0 |
| 55 | + >>> product_sum([1, -2], 1) |
| 56 | + -1 |
| 57 | + >>> product_sum([-3.5, [1, [0.5]]], 1) |
| 58 | + 1.5 |
| 59 | +
|
| 60 | + """ |
| 61 | + total_sum = 0 |
| 62 | + for ele in arr: |
| 63 | + total_sum += product_sum(ele, depth + 1) if isinstance(ele, list) else ele |
| 64 | + return total_sum * depth |
| 65 | + |
| 66 | + |
| 67 | +def product_sum_array(array: list[int | list]) -> int: |
| 68 | + """ |
| 69 | + Calculates the product sum of an array. |
| 70 | +
|
| 71 | + Args: |
| 72 | + array (List[Union[int, List]]): The array of integers and nested lists. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + int: The product sum of the array. |
| 76 | +
|
| 77 | + Examples: |
| 78 | + >>> product_sum_array([1, 2, 3]) |
| 79 | + 6 |
| 80 | + >>> product_sum_array([1, [2, 3]]) |
| 81 | + 11 |
| 82 | + >>> product_sum_array([1, [2, [3, 4]]]) |
| 83 | + 47 |
| 84 | + >>> product_sum_array([0]) |
| 85 | + 0 |
| 86 | + >>> product_sum_array([-3.5, [1, [0.5]]]) |
| 87 | + 1.5 |
| 88 | + >>> product_sum_array([1, -2]) |
| 89 | + -1 |
| 90 | +
|
| 91 | + """ |
| 92 | + return product_sum(array, 1) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + import doctest |
| 97 | + |
| 98 | + doctest.testmod() |
0 commit comments