Skip to content

Commit e2a0b79

Browse files
committed
fix: correct sample method logic; add test
1 parent eaecf39 commit e2a0b79

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

src/helpers.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { scale, sample } from './helpers';
2+
3+
describe('scale function', () => {
4+
it('should scale the array values between 0 and 1', () => {
5+
const inputArray = [1, 2, 3, 4, 5];
6+
const scaledArray = scale(inputArray);
7+
8+
expect(scaledArray).toEqual([0, 0.25, 0.5, 0.75, 1]);
9+
});
10+
11+
it('should handle an empty array', () => {
12+
const inputArray: number[] = [];
13+
const scaledArray = scale(inputArray);
14+
15+
expect(scaledArray).toEqual([]);
16+
});
17+
18+
it('should handle an array with one element', () => {
19+
const inputArray = [42];
20+
const scaledArray = scale(inputArray);
21+
22+
expect(scaledArray).toEqual([0]);
23+
});
24+
});
25+
26+
describe('sample function', () => {
27+
it('should sample the array to the specified size', () => {
28+
const inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
29+
const newSize = 5;
30+
const sampledArray = sample(inputArray, newSize);
31+
32+
expect(sampledArray.length).toBe(newSize);
33+
expect(sampledArray).toEqual([1, 3, 5, 7, 9]);
34+
});
35+
36+
it('should handle an empty array', () => {
37+
const inputArray: number[] = [];
38+
const newSize = 0;
39+
const sampledArray = sample(inputArray, newSize);
40+
41+
expect(sampledArray).toEqual([]);
42+
});
43+
44+
it('should handle an array with one element', () => {
45+
const inputArray = [42];
46+
const newSize = 5;
47+
const sampledArray = sample(inputArray, newSize);
48+
49+
expect(sampledArray).toEqual([42, 42, 42, 42, 42]); // The result will be an array with the same element repeated
50+
});
51+
52+
it('should handle newSize greater than the original size', () => {
53+
const inputArray = [1, 2, 3];
54+
const newSize = 5;
55+
const sampledArray = sample(inputArray, newSize);
56+
57+
expect(sampledArray).toEqual([1, 1, 2, 2, 3]); // The result will include elements from the original array with repetition
58+
});
59+
});

src/helpers.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
export function scale(arr: number[]) {
2+
if (arr.length === 0) {
3+
return [];
4+
}
5+
26
const min = Math.min(...arr);
37
const max = Math.max(...arr);
48
const diff = max - min;
59

6-
return arr.map((_) => (_ - min) / diff);
10+
if (diff === 0) {
11+
// If diff is 0, all elements in the array are the same.
12+
// To avoid division by zero, return an array of the same length with 0 values.
13+
return arr.map(() => 0);
14+
}
15+
16+
return arr.map((value) => (value - min) / diff);
717
}
818

919
export function sample(arr: number[], newSize: number) {
1020
const originalSize = arr.length;
11-
const ratio = originalSize / newSize;
12-
const result = [];
1321

14-
for (let i = 0; i < newSize; i++) {
15-
const startIndex = Math.floor(i * ratio);
16-
const endIndex = Math.floor((i + 1) * ratio);
17-
let sum = 0;
22+
if (newSize === 0) {
23+
return [];
24+
}
1825

19-
for (let j = startIndex; j < endIndex; j++) {
20-
sum += arr[j]!;
21-
}
26+
const result: number[] = [];
2227

23-
const average = sum / (endIndex - startIndex);
24-
result.push(average);
28+
for (let i = 0; i < newSize; i++) {
29+
const index = Math.floor((i / newSize) * originalSize);
30+
result.push(arr[index] ?? 0);
2531
}
2632

2733
return result;

0 commit comments

Comments
 (0)