@gabrielrufino/cerebrum 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/cd.yml +1 -1
- package/README.md +2 -0
- package/dist/Math/LinearRegression.js +0 -1
- package/dist/Search/BinarySearch.js +26 -0
- package/dist/Search/BinarySearch.test.js +51 -0
- package/dist/Search/LinearSearch.js +16 -0
- package/dist/Search/LinearSearch.test.js +36 -0
- package/dist/Search/Search.js +26 -0
- package/dist/Sort/BubbleSort.js +0 -1
- package/dist/Sort/SelectionSort.js +0 -1
- package/dist/index.js +4 -0
- package/package.json +4 -5
- package/src/Math/LinearRegression.ts +0 -2
- package/src/Search/BinarySearch.test.ts +63 -0
- package/src/Search/BinarySearch.ts +25 -0
- package/src/Search/LinearSearch.test.ts +44 -0
- package/src/Search/LinearSearch.ts +15 -0
- package/src/Search/Search.ts +28 -0
- package/src/Sort/BubbleSort.ts +1 -3
- package/src/Sort/SelectionSort.ts +1 -3
- package/src/index.ts +5 -0
package/.github/workflows/cd.yml
CHANGED
package/README.md
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BinarySearch = void 0;
|
|
4
|
+
const Search_1 = require("./Search");
|
|
5
|
+
class BinarySearch extends Search_1.Search {
|
|
6
|
+
execute() {
|
|
7
|
+
this.beforeExecute();
|
|
8
|
+
let left = 0;
|
|
9
|
+
let right = this.elements.length - 1;
|
|
10
|
+
while (left <= right) {
|
|
11
|
+
const middle = Math.floor((left + right) / 2);
|
|
12
|
+
const middleElement = this.elements[middle];
|
|
13
|
+
if (middleElement === this.target) {
|
|
14
|
+
return middle;
|
|
15
|
+
}
|
|
16
|
+
else if (middleElement < this.target) {
|
|
17
|
+
left = middle + 1;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
right = middle - 1;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.BinarySearch = BinarySearch;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const BinarySearch_1 = require("./BinarySearch");
|
|
5
|
+
(0, vitest_1.describe)('BinarySearch', () => {
|
|
6
|
+
(0, vitest_1.it)('should return the index of the target when it exists in the array', () => {
|
|
7
|
+
const result = new BinarySearch_1.BinarySearch()
|
|
8
|
+
.setElements([1, 2, 3, 4, 5])
|
|
9
|
+
.setTarget(3)
|
|
10
|
+
.execute();
|
|
11
|
+
(0, vitest_1.expect)(result).toBe(2);
|
|
12
|
+
});
|
|
13
|
+
(0, vitest_1.it)('should return null when the target does not exist in the array', () => {
|
|
14
|
+
const result = new BinarySearch_1.BinarySearch()
|
|
15
|
+
.setElements([1, 2, 3, 4, 5])
|
|
16
|
+
.setTarget(6)
|
|
17
|
+
.execute();
|
|
18
|
+
(0, vitest_1.expect)(result).toBeNull();
|
|
19
|
+
});
|
|
20
|
+
(0, vitest_1.it)('should throw an error if elements are not defined', () => {
|
|
21
|
+
const search = new BinarySearch_1.BinarySearch()
|
|
22
|
+
.setTarget(3);
|
|
23
|
+
(0, vitest_1.expect)(() => search.execute()).toThrow('Elements should be defined');
|
|
24
|
+
});
|
|
25
|
+
(0, vitest_1.it)('should throw an error if target is not defined', () => {
|
|
26
|
+
const search = new BinarySearch_1.BinarySearch()
|
|
27
|
+
.setElements([1, 2, 3, 4, 5]);
|
|
28
|
+
(0, vitest_1.expect)(() => search.execute()).toThrow('Target should be defined');
|
|
29
|
+
});
|
|
30
|
+
(0, vitest_1.it)('should return the index when searching for 0 in a list that contains 0', () => {
|
|
31
|
+
const result = new BinarySearch_1.BinarySearch()
|
|
32
|
+
.setElements([0, 1, 2, 3, 4, 5])
|
|
33
|
+
.setTarget(0)
|
|
34
|
+
.execute();
|
|
35
|
+
(0, vitest_1.expect)(result).toBe(0);
|
|
36
|
+
});
|
|
37
|
+
(0, vitest_1.it)('should return null if target is smaller than all elements', () => {
|
|
38
|
+
const result = new BinarySearch_1.BinarySearch()
|
|
39
|
+
.setElements([10, 20, 30, 40, 50])
|
|
40
|
+
.setTarget(5)
|
|
41
|
+
.execute();
|
|
42
|
+
(0, vitest_1.expect)(result).toBeNull();
|
|
43
|
+
});
|
|
44
|
+
(0, vitest_1.it)('should return null if target is larger than all elements', () => {
|
|
45
|
+
const result = new BinarySearch_1.BinarySearch()
|
|
46
|
+
.setElements([10, 20, 30, 40, 50])
|
|
47
|
+
.setTarget(60)
|
|
48
|
+
.execute();
|
|
49
|
+
(0, vitest_1.expect)(result).toBeNull();
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LinearSearch = void 0;
|
|
4
|
+
const Search_1 = require("./Search");
|
|
5
|
+
class LinearSearch extends Search_1.Search {
|
|
6
|
+
execute() {
|
|
7
|
+
this.beforeExecute();
|
|
8
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
9
|
+
if (this.elements[i] === this.target) {
|
|
10
|
+
return i;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.LinearSearch = LinearSearch;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const LinearSearch_1 = require("./LinearSearch");
|
|
5
|
+
(0, vitest_1.describe)('LinearSearch', () => {
|
|
6
|
+
(0, vitest_1.it)('should return the index of the target when it exists in the array', () => {
|
|
7
|
+
const result = new LinearSearch_1.LinearSearch()
|
|
8
|
+
.setElements([1, 2, 3, 4, 5])
|
|
9
|
+
.setTarget(3)
|
|
10
|
+
.execute();
|
|
11
|
+
(0, vitest_1.expect)(result).toBe(2);
|
|
12
|
+
});
|
|
13
|
+
(0, vitest_1.it)('should return null when the target does not exist in the array', () => {
|
|
14
|
+
const result = new LinearSearch_1.LinearSearch()
|
|
15
|
+
.setElements([1, 2, 3, 4, 5])
|
|
16
|
+
.setTarget(6)
|
|
17
|
+
.execute();
|
|
18
|
+
(0, vitest_1.expect)(result).toBeNull();
|
|
19
|
+
});
|
|
20
|
+
(0, vitest_1.it)('should throw an error if elements are not defined', () => {
|
|
21
|
+
const search = new LinearSearch_1.LinearSearch()
|
|
22
|
+
.setTarget(3);
|
|
23
|
+
(0, vitest_1.expect)(() => search.execute()).toThrow('Elements should be defined');
|
|
24
|
+
});
|
|
25
|
+
(0, vitest_1.it)('should throw an error if target is not defined', () => {
|
|
26
|
+
const search = new LinearSearch_1.LinearSearch()
|
|
27
|
+
.setElements([1, 2, 3, 4, 5]);
|
|
28
|
+
(0, vitest_1.expect)(() => search.execute()).toThrow('Target should be defined');
|
|
29
|
+
});
|
|
30
|
+
(0, vitest_1.it)('should return the index when target is 0', () => {
|
|
31
|
+
const result = new LinearSearch_1.LinearSearch()
|
|
32
|
+
.setElements([1, 0, 3, 4, 5]).setTarget(0)
|
|
33
|
+
.execute();
|
|
34
|
+
(0, vitest_1.expect)(result).toBe(1);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Search = void 0;
|
|
4
|
+
class Search {
|
|
5
|
+
constructor(elements, target) {
|
|
6
|
+
this.elements = elements;
|
|
7
|
+
this.target = target;
|
|
8
|
+
}
|
|
9
|
+
beforeExecute() {
|
|
10
|
+
if (!this.elements) {
|
|
11
|
+
throw new Error('Elements should be defined');
|
|
12
|
+
}
|
|
13
|
+
if (this.target === undefined || this.target === null) {
|
|
14
|
+
throw new Error('Target should be defined');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
setElements(elements) {
|
|
18
|
+
this.elements = elements;
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
setTarget(target) {
|
|
22
|
+
this.target = target;
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.Search = Search;
|
package/dist/Sort/BubbleSort.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,5 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./Math/Fibonacci"), exports);
|
|
18
|
+
__exportStar(require("./Math/LinearRegression"), exports);
|
|
19
|
+
__exportStar(require("./Math/TwoSum"), exports);
|
|
20
|
+
__exportStar(require("./Search/BinarySearch"), exports);
|
|
21
|
+
__exportStar(require("./Search/LinearSearch"), exports);
|
|
18
22
|
__exportStar(require("./Sort/BubbleSort"), exports);
|
|
19
23
|
__exportStar(require("./Sort/SelectionSort"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gabrielrufino/cerebrum",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Algorithms made in TypeScript",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -10,8 +10,7 @@
|
|
|
10
10
|
"lint:fix": "npm run lint -- --fix",
|
|
11
11
|
"test": "vitest run",
|
|
12
12
|
"test:cov": "npm test -- --coverage",
|
|
13
|
-
"test:watch": "vitest"
|
|
14
|
-
"prepare": "husky"
|
|
13
|
+
"test:watch": "vitest"
|
|
15
14
|
},
|
|
16
15
|
"author": "Gabriel Rufino <contato@gabrielrufino.com>",
|
|
17
16
|
"license": "UNLICENSED",
|
|
@@ -20,10 +19,10 @@
|
|
|
20
19
|
"@commitlint/config-conventional": "^19.5.0",
|
|
21
20
|
"@faker-js/faker": "^8.4.1",
|
|
22
21
|
"@gabrielrufino/eslint-config": "^1.6.0",
|
|
23
|
-
"@vitest/coverage-v8": "^2.1.
|
|
22
|
+
"@vitest/coverage-v8": "^2.1.3",
|
|
24
23
|
"eslint": "^8.57.1",
|
|
25
24
|
"husky": "^9.1.6",
|
|
26
|
-
"typescript": "^5.6.
|
|
25
|
+
"typescript": "^5.6.3",
|
|
27
26
|
"vitest": "^2.1.0"
|
|
28
27
|
},
|
|
29
28
|
"funding": [
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { BinarySearch } from './BinarySearch'
|
|
3
|
+
|
|
4
|
+
describe('BinarySearch', () => {
|
|
5
|
+
it('should return the index of the target when it exists in the array', () => {
|
|
6
|
+
const result = new BinarySearch()
|
|
7
|
+
.setElements([1, 2, 3, 4, 5])
|
|
8
|
+
.setTarget(3)
|
|
9
|
+
.execute()
|
|
10
|
+
|
|
11
|
+
expect(result).toBe(2)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should return null when the target does not exist in the array', () => {
|
|
15
|
+
const result = new BinarySearch()
|
|
16
|
+
.setElements([1, 2, 3, 4, 5])
|
|
17
|
+
.setTarget(6)
|
|
18
|
+
.execute()
|
|
19
|
+
|
|
20
|
+
expect(result).toBeNull()
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('should throw an error if elements are not defined', () => {
|
|
24
|
+
const search = new BinarySearch()
|
|
25
|
+
.setTarget(3)
|
|
26
|
+
|
|
27
|
+
expect(() => search.execute()).toThrow('Elements should be defined')
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('should throw an error if target is not defined', () => {
|
|
31
|
+
const search = new BinarySearch()
|
|
32
|
+
.setElements([1, 2, 3, 4, 5])
|
|
33
|
+
|
|
34
|
+
expect(() => search.execute()).toThrow('Target should be defined')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should return the index when searching for 0 in a list that contains 0', () => {
|
|
38
|
+
const result = new BinarySearch()
|
|
39
|
+
.setElements([0, 1, 2, 3, 4, 5])
|
|
40
|
+
.setTarget(0)
|
|
41
|
+
.execute()
|
|
42
|
+
|
|
43
|
+
expect(result).toBe(0)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('should return null if target is smaller than all elements', () => {
|
|
47
|
+
const result = new BinarySearch()
|
|
48
|
+
.setElements([10, 20, 30, 40, 50])
|
|
49
|
+
.setTarget(5)
|
|
50
|
+
.execute()
|
|
51
|
+
|
|
52
|
+
expect(result).toBeNull()
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('should return null if target is larger than all elements', () => {
|
|
56
|
+
const result = new BinarySearch()
|
|
57
|
+
.setElements([10, 20, 30, 40, 50])
|
|
58
|
+
.setTarget(60)
|
|
59
|
+
.execute()
|
|
60
|
+
|
|
61
|
+
expect(result).toBeNull()
|
|
62
|
+
})
|
|
63
|
+
})
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Search } from './Search';
|
|
2
|
+
|
|
3
|
+
export class BinarySearch extends Search {
|
|
4
|
+
public execute(): number | null {
|
|
5
|
+
this.beforeExecute()
|
|
6
|
+
|
|
7
|
+
let left = 0
|
|
8
|
+
let right = this.elements!.length - 1
|
|
9
|
+
|
|
10
|
+
while (left <= right) {
|
|
11
|
+
const middle = Math.floor((left + right) / 2)
|
|
12
|
+
const middleElement = this.elements![middle]
|
|
13
|
+
|
|
14
|
+
if (middleElement === this.target!) {
|
|
15
|
+
return middle
|
|
16
|
+
} else if (middleElement < this.target!) {
|
|
17
|
+
left = middle + 1
|
|
18
|
+
} else {
|
|
19
|
+
right = middle - 1
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return null
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { LinearSearch } from './LinearSearch'
|
|
3
|
+
|
|
4
|
+
describe('LinearSearch', () => {
|
|
5
|
+
it('should return the index of the target when it exists in the array', () => {
|
|
6
|
+
const result = new LinearSearch()
|
|
7
|
+
.setElements([1, 2, 3, 4, 5])
|
|
8
|
+
.setTarget(3)
|
|
9
|
+
.execute()
|
|
10
|
+
|
|
11
|
+
expect(result).toBe(2)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should return null when the target does not exist in the array', () => {
|
|
15
|
+
const result = new LinearSearch()
|
|
16
|
+
.setElements([1, 2, 3, 4, 5])
|
|
17
|
+
.setTarget(6)
|
|
18
|
+
.execute()
|
|
19
|
+
|
|
20
|
+
expect(result).toBeNull()
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('should throw an error if elements are not defined', () => {
|
|
24
|
+
const search = new LinearSearch()
|
|
25
|
+
.setTarget(3)
|
|
26
|
+
|
|
27
|
+
expect(() => search.execute()).toThrow('Elements should be defined')
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('should throw an error if target is not defined', () => {
|
|
31
|
+
const search = new LinearSearch()
|
|
32
|
+
.setElements([1, 2, 3, 4, 5])
|
|
33
|
+
|
|
34
|
+
expect(() => search.execute()).toThrow('Target should be defined')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should return the index when target is 0', () => {
|
|
38
|
+
const result = new LinearSearch()
|
|
39
|
+
.setElements([1, 0, 3, 4, 5]).setTarget(0)
|
|
40
|
+
.execute()
|
|
41
|
+
|
|
42
|
+
expect(result).toBe(1)
|
|
43
|
+
})
|
|
44
|
+
})
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Search } from './Search'
|
|
2
|
+
|
|
3
|
+
export class LinearSearch extends Search {
|
|
4
|
+
public execute (): number | null {
|
|
5
|
+
this.beforeExecute()
|
|
6
|
+
|
|
7
|
+
for (let i = 0; i < this.elements!.length; i++) {
|
|
8
|
+
if (this.elements![i] === this.target) {
|
|
9
|
+
return i
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return null
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export abstract class Search {
|
|
2
|
+
constructor (
|
|
3
|
+
protected elements?: Array<number>,
|
|
4
|
+
protected target?: number
|
|
5
|
+
) {}
|
|
6
|
+
|
|
7
|
+
protected beforeExecute () {
|
|
8
|
+
if (!this.elements) {
|
|
9
|
+
throw new Error('Elements should be defined')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (this.target === undefined || this.target === null) {
|
|
13
|
+
throw new Error('Target should be defined')
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public setElements (elements: Array<number>) {
|
|
18
|
+
this.elements = elements
|
|
19
|
+
return this
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public setTarget (target: number) {
|
|
23
|
+
this.target = target
|
|
24
|
+
return this
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public abstract execute(): number | null
|
|
28
|
+
}
|
package/src/Sort/BubbleSort.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export * from './Math/Fibonacci';
|
|
2
|
+
export * from './Math/LinearRegression';
|
|
3
|
+
export * from './Math/TwoSum';
|
|
4
|
+
|
|
5
|
+
export * from './Search/BinarySearch';
|
|
6
|
+
export * from './Search/LinearSearch';
|
|
2
7
|
|
|
3
8
|
export * from './Sort/BubbleSort';
|
|
4
9
|
export * from './Sort/SelectionSort';
|