Skip to content

Commit 331585f

Browse files
Himanshutomar31pre-commit-ci[bot]cclauss
authored
Algorithm: Calculating Product Sum from a Special Array with Nested Structures (#8761)
* Added minimum waiting time problem solution using greedy algorithm * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ruff --fix * Add type hints * Added two more doc test * Removed unnecessary comments * updated type hints * Updated the code as per the code review * Added recursive algo to calculate product sum from an array * Added recursive algo to calculate product sum from an array * Update doc string * Added doctest for product_sum function * Updated the code and added more doctests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added more test coverage for product_sum method * Update product_sum.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent f54a966 commit 331585f

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

Diff for: DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
* Arrays
167167
* [Permutations](data_structures/arrays/permutations.py)
168168
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
169+
* [Product Sum Array](data_structures/arrays/product_sum.py)
169170
* Binary Tree
170171
* [Avl Tree](data_structures/binary_tree/avl_tree.py)
171172
* [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py)

Diff for: data_structures/arrays/product_sum.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)