-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
all files formatted successfully #1788
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
Closed
karanupd12
wants to merge
1
commit into
TheAlgorithms:master
from
karanupd12:add-prefix-sum-algorithm
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Computes the prefix sum of an array | ||
* @param {number[]} arrayOfNumbers - Array of numbers | ||
* @returns {number[]} - Prefix sum array | ||
* @throws {TypeError} - If input is not a numeric array | ||
*/ | ||
function basicPrefixSum(arrayOfNumbers) { | ||
// Validate input | ||
if (!Array.isArray(arrayOfNumbers)) { | ||
throw new TypeError('Input must be an array') | ||
} | ||
|
||
for (let i = 0; i < arrayOfNumbers.length; i++) { | ||
if ( | ||
typeof arrayOfNumbers[i] !== 'number' || | ||
!Number.isFinite(arrayOfNumbers[i]) | ||
) { | ||
throw new TypeError('All elements must be finite numbers') | ||
} | ||
} | ||
|
||
// Handle empty array | ||
if (arrayOfNumbers.length === 0) { | ||
return [] | ||
} | ||
|
||
const prefixSum = new Array(arrayOfNumbers.length) | ||
prefixSum[0] = arrayOfNumbers[0] | ||
|
||
for (let i = 1; i < arrayOfNumbers.length; i++) { | ||
prefixSum[i] = prefixSum[i - 1] + arrayOfNumbers[i] | ||
} | ||
|
||
return prefixSum | ||
} | ||
|
||
export default basicPrefixSum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { describe, it, expect } from 'vitest' | ||
import basicPrefixSum from './BasicPrefixSum.js' | ||
|
||
describe('BasicPrefixSum', () => { | ||
it('should compute prefix sum for normal array', () => { | ||
const input = [1, 2, 3, 4, 5] | ||
const expected = [1, 3, 6, 10, 15] | ||
expect(basicPrefixSum(input)).toEqual(expected) | ||
}) | ||
|
||
it('should handle empty array', () => { | ||
expect(basicPrefixSum([])).toEqual([]) | ||
}) | ||
|
||
it('should handle single element array', () => { | ||
expect(basicPrefixSum([5])).toEqual([5]) | ||
}) | ||
|
||
it('should handle negative numbers', () => { | ||
const input = [-1, 2, -3, 4] | ||
const expected = [-1, 1, -2, 2] | ||
expect(basicPrefixSum(input)).toEqual(expected) | ||
}) | ||
|
||
it('should throw TypeError for non-array input', () => { | ||
expect(() => basicPrefixSum('not an array')).toThrow(TypeError) | ||
expect(() => basicPrefixSum(123)).toThrow(TypeError) | ||
expect(() => basicPrefixSum(null)).toThrow(TypeError) | ||
}) | ||
|
||
it('should throw TypeError for non-numeric array elements', () => { | ||
expect(() => basicPrefixSum([1, 'string', 3])).toThrow(TypeError) | ||
expect(() => basicPrefixSum([1, null, 3])).toThrow(TypeError) | ||
expect(() => basicPrefixSum([1, undefined, 3])).toThrow(TypeError) | ||
}) | ||
|
||
it('should throw TypeError for infinite values', () => { | ||
expect(() => basicPrefixSum([1, Infinity, 3])).toThrow(TypeError) | ||
expect(() => basicPrefixSum([1, -Infinity, 3])).toThrow(TypeError) | ||
expect(() => basicPrefixSum([1, NaN, 3])).toThrow(TypeError) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.