mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-24 18:08:39 +00:00
Compare commits
2 Commits
f54a966810
...
267a8b72f9
Author | SHA1 | Date | |
---|---|---|---|
|
267a8b72f9 | ||
|
331585f3f8 |
2
.github/pull_request_template.md
vendored
2
.github/pull_request_template.md
vendored
@ -17,4 +17,4 @@
|
||||
* [ ] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
|
||||
* [ ] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
|
||||
* [ ] All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
|
||||
* [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.
|
||||
* [ ] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a [closing keyword](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue): "Fixes #ISSUE-NUMBER".
|
||||
|
@ -25,7 +25,12 @@ We appreciate any contribution, from fixing a grammar mistake in a comment to im
|
||||
|
||||
Your contribution will be tested by our [automated testing on GitHub Actions](https://github.com/TheAlgorithms/Python/actions) to save time and mental energy. After you have submitted your pull request, you should see the GitHub Actions tests start to run at the bottom of your submission page. If those tests fail, then click on the ___details___ button try to read through the GitHub Actions output to understand the failure. If you do not understand, please leave a comment on your submission page and a community member will try to help.
|
||||
|
||||
Please help us keep our issue list small by adding fixes: #{$ISSUE_NO} to the commit message of pull requests that resolve open issues. GitHub will use this tag to auto-close the issue when the PR is merged.
|
||||
Please help us keep our issue list small by adding `Fixes #{$ISSUE_NUMBER}` to the description of pull requests that resolve open issues.
|
||||
For example, if your pull request fixes issue #10, then please add the following to its description:
|
||||
```
|
||||
Fixes #10
|
||||
```
|
||||
GitHub will use this tag to [auto-close the issue](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) if and when the PR is merged.
|
||||
|
||||
#### What is an Algorithm?
|
||||
|
||||
|
@ -146,6 +146,7 @@
|
||||
* [Decimal To Binary Recursion](conversions/decimal_to_binary_recursion.py)
|
||||
* [Decimal To Hexadecimal](conversions/decimal_to_hexadecimal.py)
|
||||
* [Decimal To Octal](conversions/decimal_to_octal.py)
|
||||
* [Energy Conversions](conversions/energy_conversions.py)
|
||||
* [Excel Title To Column](conversions/excel_title_to_column.py)
|
||||
* [Hex To Bin](conversions/hex_to_bin.py)
|
||||
* [Hexadecimal To Decimal](conversions/hexadecimal_to_decimal.py)
|
||||
@ -166,6 +167,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)
|
||||
@ -410,6 +412,7 @@
|
||||
* [Dijkstra 2](graphs/dijkstra_2.py)
|
||||
* [Dijkstra Algorithm](graphs/dijkstra_algorithm.py)
|
||||
* [Dijkstra Alternate](graphs/dijkstra_alternate.py)
|
||||
* [Dijkstra Binary Grid](graphs/dijkstra_binary_grid.py)
|
||||
* [Dinic](graphs/dinic.py)
|
||||
* [Directed And Undirected (Weighted) Graph](graphs/directed_and_undirected_(weighted)_graph.py)
|
||||
* [Edmonds Karp Multiple Source And Sink](graphs/edmonds_karp_multiple_source_and_sink.py)
|
||||
|
98
data_structures/arrays/product_sum.py
Normal file
98
data_structures/arrays/product_sum.py
Normal file
@ -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:
|
||||
"""
|
||||
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
|
||||
>>> 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]])
|
||||
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()
|
Loading…
x
Reference in New Issue
Block a user