Skip to content

Algorithm: Calculating Product Sum from a Special Array with Nested Structures #8761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0b75e10
Added minimum waiting time problem solution using greedy algorithm
Himanshutomar31 Apr 29, 2023
6520b7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 29, 2023
bd51b5a
ruff --fix
Himanshutomar31 Apr 29, 2023
4e9e136
Merge branch 'master' of https://github.com/Himanshutomar31/Python-1
Himanshutomar31 Apr 29, 2023
0bd440e
Add type hints
Himanshutomar31 Apr 29, 2023
ac29bff
Added two more doc test
Himanshutomar31 Apr 30, 2023
68fa2da
Merge branch 'TheAlgorithms:master' into master
Himanshutomar31 Apr 30, 2023
8062a3c
Removed unnecessary comments
Himanshutomar31 May 1, 2023
727d449
Merge branch 'master' of https://github.com/Himanshutomar31/Python-1
Himanshutomar31 May 1, 2023
27a0d4a
updated type hints
Himanshutomar31 May 1, 2023
b2bf988
Updated the code as per the code review
Himanshutomar31 May 1, 2023
7ce741d
Added recursive algo to calculate product sum from an array
Himanshutomar31 May 24, 2023
c3e3e4c
Merge branch 'TheAlgorithms:master' into master
Himanshutomar31 May 24, 2023
e56303e
Added recursive algo to calculate product sum from an array
Himanshutomar31 May 24, 2023
1ca9b66
Update doc string
Himanshutomar31 May 28, 2023
a10f861
Merge branch 'TheAlgorithms:master' into master
Himanshutomar31 May 29, 2023
f1f8f67
Added doctest for product_sum function
Himanshutomar31 May 29, 2023
761b609
Merge branch 'master' of https://github.com/Himanshutomar31/Python-1
Himanshutomar31 May 29, 2023
0f46c07
Updated the code and added more doctests
Himanshutomar31 May 29, 2023
340e3f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 29, 2023
6b9f712
Added more test coverage for product_sum method
Himanshutomar31 Jun 9, 2023
465a41f
Merge branch 'master' of https://github.com/Himanshutomar31/Python-1
Himanshutomar31 Jun 9, 2023
4fae370
Update product_sum.py
cclauss Jun 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
* Arrays
* [Permutations](data_structures/arrays/permutations.py)
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
* [Product Sum Array](data_structures/arrays/product_sum.py)
* Binary Tree
* [Avl Tree](data_structures/binary_tree/avl_tree.py)
* [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py)
Expand Down
77 changes: 77 additions & 0 deletions data_structures/arrays/product_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Calculate Product Sum from an Array.
reference: https://dev.to/sfrasica/algorithms-product-sum-from-an-array-dc6

For doctests run following command:
python -m doctest -v product_sum.py

We need to create a function that calculates the product sum of a "special" array.
This array can contain integers or nested arrays. The product sum is obtained by
adding all the elements together and multiplying by their respective depth.

For example, in the array [x, y], the product sum is (x + y). In the array [x, [y, z]],
the product sum is x + 2 * (y + z). In the array [x, [y, [z]]],
the product sum is x + 2 * (y + 3z).

Example Input:
[5, 2, [-7, 1], 3, [6, [-13, 8], 4]]
Output: 12

"""


def product_sum(arr: list[int | list], depth: int) -> int:
"""
Recursively calculates the product sum of an array.

The product sum of an array is defined as the sum of its elements multiplied by
their respective depths.If an element is a list, its product sum is calculated
recursively by multiplying the sum of its elements with its depth plus one.

Args:
arr: The array of integers and nested lists.
depth: The current depth level.

Returns:
int: The product sum of the array.

Examples:
>>> product_sum([1, 2, 3], 1)
6
>>> product_sum([-1, 2, [-3, 4]], 2)
8
"""
total_sum = 0
for ele in arr:
if isinstance(ele, list):
total_sum += product_sum(ele, depth + 1)
else:
total_sum += ele
return total_sum * depth


def product_sum_array(array: list[int | list]) -> int:
"""
Calculates the product sum of an array.

Args:
array (List[Union[int, List]]): The array of integers and nested lists.

Returns:
int: The product sum of the array.

Examples:
>>> product_sum_array([1, 2, 3])
6
>>> product_sum_array([1, [2, 3]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some tests with zero and negative integer and 1.5.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I have updated the code as per the suggestions, can you please review

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need product_sum_array() tests with zero and negative integer and 1.5.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I have added the doctests for the product_sum_array() function, can you please review

11
>>> product_sum_array([1, [2, [3, 4]]])
47
"""
return product_sum(array, 1)


if __name__ == "__main__":
import doctest

doctest.testmod()