Skip to content
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

Feature/new recursion algorithms #2

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -12,6 +12,7 @@

- **Recursion**
- [NthFibonacci](Recursion/NthFibonacci.js)
- [ProductSum](Recursion/ProductSum.js)

- **Searching**
- [BinarySearch](Searching/BinarySearch.js)
Expand Down
47 changes: 47 additions & 0 deletions Recursion/ProductSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Calculate the product sum of a "special" array.
*
* @param {Array} array - The input "special" array to calculate the product sum for.
* @param {number} depthLevelMultiplier - (Optional) The depth level multiplier used to calculate the depth.
* @returns {number} - The product sum of the "special" array.
*
* @description
* A "special" array is a non-empty array that contains either integers or other "special" arrays.
* The product sum of a "special" array is the sum of its elements, where "special" arrays inside it
* are summed themselves and then multiplied by their level of depth.
*
* The depth of a "special" array is how far nested it is. For instance, the depth of [] is 1; the depth
* of the inner array in [[J] is 2; the depth of the innermost array in [CEll is 3.
*
* The function uses recursion to traverse the nested arrays, calculating the product sum of each nested
* array and multiplying it by its depth level. It then returns the product sum of the entire "special" array.
*
* @Complexity
* - Time Complexity: O(n), where n is the total number of elements in the "special" array.
* - Space Complexity: O(d), where d is the maximum depth of nesting in the "special" array.
*/
function productSum(array, depthLevelMultiplier = 1) {
// Initialize the sum to 0 for this level of the "special" array.
let sum = 0;

// Iterate through the elements in the input array.
for (let i = 0; i < array.length; i++) {
// If the element is not an array, add it to the sum.
if (!Array.isArray(array[i])) {
sum += array[i];
} else {
// If the element is a "special" array, calculate its depth level.
// The depth level is increased by 1 for each level of nesting.
let depthLevel = depthLevelMultiplier + 1;

// Recursively calculate the product sum of the nested "special" array.
// Multiply the product sum of the nested array by its depth level and add it to the sum.
sum += depthLevel * productSum(array[i], depthLevel);
}
}

// Return the calculated product sum for the current level.
return sum;
}

export { productSum };
43 changes: 43 additions & 0 deletions Recursion/tests/ProductSum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { productSum } from "../ProductSum";

describe("Product Sum", () => {
it("should calculate product sum for a nested array", () => {
const array = [5, 2, [7, -1], 3, [6, [-13, 8], 4]];
expect(productSum(array)).toBe(12);
});

it("should calculate product sum for a simple array", () => {
const array = [1, 2, 3, 4, 5];
expect(productSum(array)).toBe(15);
});

it("should calculate product sum for a partially nested array", () => {
const array = [1, 2, [3], 4, 5];
expect(productSum(array)).toBe(18);
});

it("should calculate product sum for a deeply nested array", () => {
const array = [[1, 2], 3, [4, 5]];
expect(productSum(array)).toBe(27);
});

it("should calculate product sum for an extremely deeply nested array", () => {
const array = [[[[[5]]]]];
expect(productSum(array)).toBe(600);
});

it("should calculate product sum for a complex nested array", () => {
const array = [
9,
[2, -3, 4],
1,
[1, 1, [1, 1, 1]],
[[[[3, 4, 1]]], 8],
[1, 2, 3, 4, 5, [6, 7], -7],
[1, [2, 3, [4, 5]], [6, 0, [7, 0, -8]], -7],
[1, -3, 2, [1, -3, 2, [1, -3, 2], [1, -3, 2, [1, -3, 2]], [1, -3, 2]]],
-3,
];
expect(productSum(array)).toBe(1351);
});
});