From eeac64481a27b495972dd5e294e67036c9107746 Mon Sep 17 00:00:00 2001 From: Dhruv Joysar Date: Wed, 15 Oct 2025 00:40:05 +0530 Subject: [PATCH 1/2] Feat: Implement recursive Max Depth Sum calculation for nested arrays --- .../arrays/list_maximum_depth_sum.py | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 data_structures/arrays/list_maximum_depth_sum.py diff --git a/data_structures/arrays/list_maximum_depth_sum.py b/data_structures/arrays/list_maximum_depth_sum.py new file mode 100644 index 000000000000..702060d69d3c --- /dev/null +++ b/data_structures/arrays/list_maximum_depth_sum.py @@ -0,0 +1,67 @@ +def calculate_max_depth_sum(array: list) -> int: + """ + Calculates the maximum sum found at any single depth level in a nested array. + """ + # Use a dictionary to store the total sum at each depth level + depth_sums = {} + + def find_sums(arr: list, depth: int): + """ + Recursive helper to populate the depth_sums dictionary. + """ + # Initialize the sum for the current depth if it doesn't exist + depth_sums[depth] = depth_sums.get(depth, 0) + + for element in arr: + if isinstance(element, list): + # Recurse: Go to the next depth level + find_sums(element, depth + 1) + else: + # Base Case: Add the integer to the sum of the current depth + depth_sums[depth] += element + + # Start the recursion from the top-level (depth 1) + find_sums(array, 1) + + # After recursion completes, return the maximum value from all stored depth sums + return max(depth_sums.values()) + + +if __name__ == "__main__": + import doctest + + # Doctests for the new problem + print("--- Max Depth Sum Examples ---") + + # Example 1: Max sum at depth 2 (4+5=9) + # >>> calculate_max_depth_sum([1, [4, 5], 2]) + # 9 + + # Example 2: Max sum at depth 1 (10) + # >>> calculate_max_depth_sum([10, [1, [2, 3]]]) + # 10 + + # Example 3: Max sum at depth 3 (100) + # >>> calculate_max_depth_sum([2, [1, [100], 1], 2]) + # 100 + + # Example 4: Nested zeroes and negatives + # The max sum can still be negative if all levels are negative + # >>> calculate_max_depth_sum([-1, [-5, -2], -1]) + # -2 # (-1 + -1) vs (-5 + -2) = -7. Max is -2. + + # Since the prompt asks for a simpler problem, I will only include basic doctests + # but the implementation provided above works for the complex examples. + + # Running basic doctests for validation + class MaxDepthSumTests: + """ + Tests for calculate_max_depth_sum + >>> calculate_max_depth_sum([1, [4, 5], 2]) + 9 + >>> calculate_max_depth_sum([10, [1, [2, 3]]]) + 10 + """ + pass + + doctest.run_docstring_examples(MaxDepthSumTests, globals()) \ No newline at end of file From 3ed04d158eb77dcc120309d10efbb8b27e9dbf5e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Oct 2025 19:31:49 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/list_maximum_depth_sum.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/data_structures/arrays/list_maximum_depth_sum.py b/data_structures/arrays/list_maximum_depth_sum.py index 702060d69d3c..d719dbac617e 100644 --- a/data_structures/arrays/list_maximum_depth_sum.py +++ b/data_structures/arrays/list_maximum_depth_sum.py @@ -32,19 +32,19 @@ def find_sums(arr: list, depth: int): # Doctests for the new problem print("--- Max Depth Sum Examples ---") - + # Example 1: Max sum at depth 2 (4+5=9) # >>> calculate_max_depth_sum([1, [4, 5], 2]) # 9 - + # Example 2: Max sum at depth 1 (10) # >>> calculate_max_depth_sum([10, [1, [2, 3]]]) # 10 - + # Example 3: Max sum at depth 3 (100) # >>> calculate_max_depth_sum([2, [1, [100], 1], 2]) # 100 - + # Example 4: Nested zeroes and negatives # The max sum can still be negative if all levels are negative # >>> calculate_max_depth_sum([-1, [-5, -2], -1]) @@ -52,7 +52,7 @@ def find_sums(arr: list, depth: int): # Since the prompt asks for a simpler problem, I will only include basic doctests # but the implementation provided above works for the complex examples. - + # Running basic doctests for validation class MaxDepthSumTests: """ @@ -62,6 +62,7 @@ class MaxDepthSumTests: >>> calculate_max_depth_sum([10, [1, [2, 3]]]) 10 """ + pass - - doctest.run_docstring_examples(MaxDepthSumTests, globals()) \ No newline at end of file + + doctest.run_docstring_examples(MaxDepthSumTests, globals())