Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 51k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
0b75e106520b7bbd51b5a4e9e1360bd440eac29bff68fa2da8062a3c727d44927a0d4ab2bf9887ce741dc3e3e4ce56303e1ca9b66a10f861f1f8f67761b6090f46c07340e3f86b9f712465a41f4fae370File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """ | ||
| Calculate the Product Sum from a Special Array. | ||
| reference: https://dev.to/sfrasica/algorithms-product-sum-from-an-array-dc6 | ||
| Python doctests can be run with the following command: | ||
| python -m doctest -v product_sum.py | ||
| Calculate the product sum of a "special" array which can contain integers or nested | ||
| arrays. The product sum is obtained by adding all elements and multiplying by their | ||
| respective depths. | ||
| 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: | ||
Himanshutomar31 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """ | ||
| 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) | ||
Himanshutomar31 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 6 | ||
| >>> product_sum([-1, 2, [-3, 4]], 2) | ||
| 8 | ||
| >>> product_sum([1, 2, 3], -1) | ||
| -6 | ||
| >>> product_sum([1, 2, 3], 0) | ||
| 0 | ||
| >>> product_sum([1, 2, 3], 7) | ||
| 42 | ||
| >>> product_sum((1, 2, 3), 7) | ||
| 42 | ||
| >>> product_sum({1, 2, 3}, 7) | ||
| 42 | ||
| >>> product_sum([1, -1], 1) | ||
| 0 | ||
| >>> product_sum([1, -2], 1) | ||
| -1 | ||
| >>> product_sum([-3.5, [1, [0.5]]], 1) | ||
| 1.5 | ||
| """ | ||
| total_sum = 0 | ||
| for ele in arr: | ||
| total_sum += product_sum(ele, depth + 1) if isinstance(ele, list) else 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]]) | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some tests with zero and negative integer and 1.5. ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still need ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| >>> product_sum_array([0]) | ||
| 0 | ||
| >>> product_sum_array([-3.5, [1, [0.5]]]) | ||
| 1.5 | ||
| >>> product_sum_array([1, -2]) | ||
| -1 | ||
| """ | ||
| return product_sum(array, 1) | ||
| if __name__ == "__main__": | ||
| import doctest | ||
| doctest.testmod() | ||
Uh oh!
There was an error while loading. Please reload this page.