From 7fbaa5fdf0c065390b45c716a97804957b3c6e9f Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Sat, 29 Oct 2022 01:18:50 +0530 Subject: [PATCH 01/10] Create maximum_subsequence.py --- other/maximum_subsequence.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 other/maximum_subsequence.py diff --git a/other/maximum_subsequence.py b/other/maximum_subsequence.py new file mode 100644 index 000000000000..26488dd7211c --- /dev/null +++ b/other/maximum_subsequence.py @@ -0,0 +1,31 @@ +from collections.abc import Sequence + + +def max_subsequence_sum(nums: Sequence[int]) -> int: + """Return the maximum possible sum amongst all non - empty subsequences. + + Raises: + ValueError: when nums is empty. + + >>> max_subsequence_sum([1,2,3,4,-2]) + 10 + >>> max_subsequence_sum([-2, -3, -1, -4, -6]) + -1 + """ + if not nums: + raise ValueError("Input sequence should not be empty") + + ans = nums[0] + nums_len = len(nums) + + for i in range(1, nums_len): + num = nums[i] + ans = max(ans, ans + num, num) + + return ans + + +if __name__ == "__main__": + n = int(input("Enter number of elements : ").strip()) + array = list(map(int, input("\nEnter the numbers : ").strip().split()))[:n] + print(max_subsequence_sum(array)) From 6f1574971273e5918545265aecd9603a12305cec Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Tue, 1 Nov 2022 20:44:28 +0530 Subject: [PATCH 02/10] Update other/maximum_subsequence.py Co-authored-by: Caeden Perelli-Harris --- other/maximum_subsequence.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/other/maximum_subsequence.py b/other/maximum_subsequence.py index 26488dd7211c..544f27aebba9 100644 --- a/other/maximum_subsequence.py +++ b/other/maximum_subsequence.py @@ -16,9 +16,7 @@ def max_subsequence_sum(nums: Sequence[int]) -> int: raise ValueError("Input sequence should not be empty") ans = nums[0] - nums_len = len(nums) - - for i in range(1, nums_len): + for i in range(1, len(nums)): num = nums[i] ans = max(ans, ans + num, num) From 2df68a82e6ea45b353c41cc0fb912e21c24c4db7 Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Wed, 2 Nov 2022 00:06:45 +0530 Subject: [PATCH 03/10] Update maximum_subsequence.py --- other/maximum_subsequence.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/other/maximum_subsequence.py b/other/maximum_subsequence.py index 544f27aebba9..052ebaf0f572 100644 --- a/other/maximum_subsequence.py +++ b/other/maximum_subsequence.py @@ -1,7 +1,8 @@ +from typing import Optional from collections.abc import Sequence -def max_subsequence_sum(nums: Sequence[int]) -> int: +def max_subsequence_sum(nums: Optional[Sequence[int]] = None) -> int: """Return the maximum possible sum amongst all non - empty subsequences. Raises: @@ -11,8 +12,16 @@ def max_subsequence_sum(nums: Sequence[int]) -> int: 10 >>> max_subsequence_sum([-2, -3, -1, -4, -6]) -1 + >>> max_subsequence_sum([]) + Traceback (most recent call last): + . . . + ValueError: Input sequence should not be empty + >>> max_subsequence_sum() + Traceback (most recent call last): + . . . + ValueError: Input sequence should not be empty """ - if not nums: + if nums is None or not nums: raise ValueError("Input sequence should not be empty") ans = nums[0] @@ -24,6 +33,10 @@ def max_subsequence_sum(nums: Sequence[int]) -> int: if __name__ == "__main__": + import doctest + doctest.testmod() + + # Try on a sample input from the user n = int(input("Enter number of elements : ").strip()) array = list(map(int, input("\nEnter the numbers : ").strip().split()))[:n] print(max_subsequence_sum(array)) From dedd91c0f484f2b424e1f2faec4671500ff4caf0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Nov 2022 18:38:22 +0000 Subject: [PATCH 04/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/maximum_subsequence.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/other/maximum_subsequence.py b/other/maximum_subsequence.py index 052ebaf0f572..7fe556d40f59 100644 --- a/other/maximum_subsequence.py +++ b/other/maximum_subsequence.py @@ -1,8 +1,8 @@ -from typing import Optional from collections.abc import Sequence +from typing import Optional -def max_subsequence_sum(nums: Optional[Sequence[int]] = None) -> int: +def max_subsequence_sum(nums: Sequence[int] | None = None) -> int: """Return the maximum possible sum amongst all non - empty subsequences. Raises: @@ -34,6 +34,7 @@ def max_subsequence_sum(nums: Optional[Sequence[int]] = None) -> int: if __name__ == "__main__": import doctest + doctest.testmod() # Try on a sample input from the user From 0dcf25e1a2410de20636d6ed5e1f0f3f908f1e22 Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Wed, 2 Nov 2022 00:12:02 +0530 Subject: [PATCH 05/10] Update maximum_subsequence.py --- other/maximum_subsequence.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/other/maximum_subsequence.py b/other/maximum_subsequence.py index 7fe556d40f59..015704221282 100644 --- a/other/maximum_subsequence.py +++ b/other/maximum_subsequence.py @@ -1,8 +1,7 @@ from collections.abc import Sequence -from typing import Optional -def max_subsequence_sum(nums: Sequence[int] | None = None) -> int: +def max_subsequence_sum(nums: Sequence[int] | None) -> int: """Return the maximum possible sum amongst all non - empty subsequences. Raises: @@ -34,7 +33,7 @@ def max_subsequence_sum(nums: Sequence[int] | None = None) -> int: if __name__ == "__main__": import doctest - + doctest.testmod() # Try on a sample input from the user From e1db64609c87b92e3fa314ac803ceccf94bad23e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 1 Nov 2022 18:42:57 +0000 Subject: [PATCH 06/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/maximum_subsequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/maximum_subsequence.py b/other/maximum_subsequence.py index 015704221282..c7f0895757ea 100644 --- a/other/maximum_subsequence.py +++ b/other/maximum_subsequence.py @@ -33,7 +33,7 @@ def max_subsequence_sum(nums: Sequence[int] | None) -> int: if __name__ == "__main__": import doctest - + doctest.testmod() # Try on a sample input from the user From 39bd75da6e9a3469be0200ae3e9f2ac040623e01 Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Wed, 2 Nov 2022 00:19:57 +0530 Subject: [PATCH 07/10] Update maximum_subsequence.py --- other/maximum_subsequence.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/maximum_subsequence.py b/other/maximum_subsequence.py index c7f0895757ea..f81717596532 100644 --- a/other/maximum_subsequence.py +++ b/other/maximum_subsequence.py @@ -1,7 +1,7 @@ from collections.abc import Sequence -def max_subsequence_sum(nums: Sequence[int] | None) -> int: +def max_subsequence_sum(nums: Sequence[int] | None = None) -> int: """Return the maximum possible sum amongst all non - empty subsequences. Raises: From 4744d96a8e2f94c91521a254dae5ee4ab7ab7e34 Mon Sep 17 00:00:00 2001 From: Pronoy Mandal Date: Thu, 3 Nov 2022 20:35:29 +0530 Subject: [PATCH 08/10] Update maximum_subsequence.py --- other/maximum_subsequence.py | 1 - 1 file changed, 1 deletion(-) diff --git a/other/maximum_subsequence.py b/other/maximum_subsequence.py index f81717596532..1c6fe36514ee 100644 --- a/other/maximum_subsequence.py +++ b/other/maximum_subsequence.py @@ -33,7 +33,6 @@ def max_subsequence_sum(nums: Sequence[int] | None = None) -> int: if __name__ == "__main__": import doctest - doctest.testmod() # Try on a sample input from the user From 5cf74a9b777300530afcc1287d5c5636a9aa99c8 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 3 Nov 2022 15:06:59 +0000 Subject: [PATCH 09/10] updating DIRECTORY.md --- DIRECTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7621427a6c34..6efbd7744c2a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -165,6 +165,7 @@ * [Binary Search Tree Recursive](data_structures/binary_tree/binary_search_tree_recursive.py) * [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py) * [Binary Tree Node Sum](data_structures/binary_tree/binary_tree_node_sum.py) + * [Binary Tree Path Sum](data_structures/binary_tree/binary_tree_path_sum.py) * [Binary Tree Traversals](data_structures/binary_tree/binary_tree_traversals.py) * [Diff Views Of Binary Tree](data_structures/binary_tree/diff_views_of_binary_tree.py) * [Fenwick Tree](data_structures/binary_tree/fenwick_tree.py) @@ -285,6 +286,7 @@ * [Bitmask](dynamic_programming/bitmask.py) * [Catalan Numbers](dynamic_programming/catalan_numbers.py) * [Climbing Stairs](dynamic_programming/climbing_stairs.py) + * [Combination Sum Iv](dynamic_programming/combination_sum_iv.py) * [Edit Distance](dynamic_programming/edit_distance.py) * [Factorial](dynamic_programming/factorial.py) * [Fast Fibonacci](dynamic_programming/fast_fibonacci.py) @@ -595,6 +597,7 @@ * [P Series](maths/series/p_series.py) * [Sieve Of Eratosthenes](maths/sieve_of_eratosthenes.py) * [Sigmoid](maths/sigmoid.py) + * [Sigmoid Linear Unit](maths/sigmoid_linear_unit.py) * [Signum](maths/signum.py) * [Simpson Rule](maths/simpson_rule.py) * [Sin](maths/sin.py) @@ -660,6 +663,7 @@ * [Lru Cache](other/lru_cache.py) * [Magicdiamondpattern](other/magicdiamondpattern.py) * [Maximum Subarray](other/maximum_subarray.py) + * [Maximum Subsequence](other/maximum_subsequence.py) * [Nested Brackets](other/nested_brackets.py) * [Password Generator](other/password_generator.py) * [Scoring Algorithm](other/scoring_algorithm.py) @@ -1107,6 +1111,7 @@ * [Fetch Jobs](web_programming/fetch_jobs.py) * [Fetch Quotes](web_programming/fetch_quotes.py) * [Fetch Well Rx Price](web_programming/fetch_well_rx_price.py) + * [Get Amazon Product Data](web_programming/get_amazon_product_data.py) * [Get Imdb Top 250 Movies Csv](web_programming/get_imdb_top_250_movies_csv.py) * [Get Imdbtop](web_programming/get_imdbtop.py) * [Get Top Billioners](web_programming/get_top_billioners.py) From 7a9e3b35416356f763f0e732eeadbe7cefdc194e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Nov 2022 15:07:56 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/maximum_subsequence.py | 1 + 1 file changed, 1 insertion(+) diff --git a/other/maximum_subsequence.py b/other/maximum_subsequence.py index 1c6fe36514ee..f81717596532 100644 --- a/other/maximum_subsequence.py +++ b/other/maximum_subsequence.py @@ -33,6 +33,7 @@ def max_subsequence_sum(nums: Sequence[int] | None = None) -> int: if __name__ == "__main__": import doctest + doctest.testmod() # Try on a sample input from the user