Skip to content

Commit a92fe31

Browse files
committed
all files formatted successfully
1 parent 1d252d7 commit a92fe31

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

PrefixSum/BasicPrefixSum.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Computes the prefix sum of an array
3+
* @param {number[]} arrayOfNumbers - Array of numbers
4+
* @returns {number[]} - Prefix sum array
5+
* @throws {TypeError} - If input is not a numeric array
6+
*/
7+
function basicPrefixSum(arrayOfNumbers) {
8+
// Validate input
9+
if (!Array.isArray(arrayOfNumbers)) {
10+
throw new TypeError('Input must be an array')
11+
}
12+
13+
for (let i = 0; i < arrayOfNumbers.length; i++) {
14+
if (
15+
typeof arrayOfNumbers[i] !== 'number' ||
16+
!Number.isFinite(arrayOfNumbers[i])
17+
) {
18+
throw new TypeError('All elements must be finite numbers')
19+
}
20+
}
21+
22+
// Handle empty array
23+
if (arrayOfNumbers.length === 0) {
24+
return []
25+
}
26+
27+
const prefixSum = new Array(arrayOfNumbers.length)
28+
prefixSum[0] = arrayOfNumbers[0]
29+
30+
for (let i = 1; i < arrayOfNumbers.length; i++) {
31+
prefixSum[i] = prefixSum[i - 1] + arrayOfNumbers[i]
32+
}
33+
34+
return prefixSum
35+
}
36+
37+
export default basicPrefixSum

PrefixSum/BasicPrefixSum.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, it, expect } from 'vitest'
2+
import basicPrefixSum from './BasicPrefixSum.js'
3+
4+
describe('BasicPrefixSum', () => {
5+
it('should compute prefix sum for normal array', () => {
6+
const input = [1, 2, 3, 4, 5]
7+
const expected = [1, 3, 6, 10, 15]
8+
expect(basicPrefixSum(input)).toEqual(expected)
9+
})
10+
11+
it('should handle empty array', () => {
12+
expect(basicPrefixSum([])).toEqual([])
13+
})
14+
15+
it('should handle single element array', () => {
16+
expect(basicPrefixSum([5])).toEqual([5])
17+
})
18+
19+
it('should handle negative numbers', () => {
20+
const input = [-1, 2, -3, 4]
21+
const expected = [-1, 1, -2, 2]
22+
expect(basicPrefixSum(input)).toEqual(expected)
23+
})
24+
25+
it('should throw TypeError for non-array input', () => {
26+
expect(() => basicPrefixSum('not an array')).toThrow(TypeError)
27+
expect(() => basicPrefixSum(123)).toThrow(TypeError)
28+
expect(() => basicPrefixSum(null)).toThrow(TypeError)
29+
})
30+
31+
it('should throw TypeError for non-numeric array elements', () => {
32+
expect(() => basicPrefixSum([1, 'string', 3])).toThrow(TypeError)
33+
expect(() => basicPrefixSum([1, null, 3])).toThrow(TypeError)
34+
expect(() => basicPrefixSum([1, undefined, 3])).toThrow(TypeError)
35+
})
36+
37+
it('should throw TypeError for infinite values', () => {
38+
expect(() => basicPrefixSum([1, Infinity, 3])).toThrow(TypeError)
39+
expect(() => basicPrefixSum([1, -Infinity, 3])).toThrow(TypeError)
40+
expect(() => basicPrefixSum([1, NaN, 3])).toThrow(TypeError)
41+
})
42+
})

0 commit comments

Comments
 (0)