@gabrielrufino/cerebrum 1.0.0 → 1.0.1
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/dist/Math/Fibonacci.js +16 -0
- package/dist/Math/LinearRegression.js +41 -0
- package/dist/Math/TwoSum.js +27 -0
- package/dist/Sort/BubbleSort.js +10 -7
- package/dist/Sort/SelectionSort.js +22 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Fibonacci = void 0;
|
|
4
|
+
class Fibonacci {
|
|
5
|
+
constructor(n) {
|
|
6
|
+
this.n = n;
|
|
7
|
+
if (n < 1)
|
|
8
|
+
throw new RangeError('Input should be a positive integer');
|
|
9
|
+
}
|
|
10
|
+
execute() {
|
|
11
|
+
if ([1, 2].includes(this.n))
|
|
12
|
+
return 1;
|
|
13
|
+
return new Fibonacci(this.n - 1).execute() + new Fibonacci(this.n - 2).execute();
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.Fibonacci = Fibonacci;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LinearRegression = void 0;
|
|
4
|
+
class LinearRegression {
|
|
5
|
+
constructor(values) {
|
|
6
|
+
this.values = values;
|
|
7
|
+
this._slope = null;
|
|
8
|
+
this._intercept = null;
|
|
9
|
+
if (values.length === 0) {
|
|
10
|
+
throw new Error('The input data must not be empty.');
|
|
11
|
+
}
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
calculate() {
|
|
15
|
+
const length = this.values.length;
|
|
16
|
+
const sumX = this.values.reduce((acc, curr) => acc + curr.x, 0);
|
|
17
|
+
const sumY = this.values.reduce((acc, curr) => acc + curr.y, 0);
|
|
18
|
+
const sumXY = this.values.reduce((acc, curr) => acc + curr.x * curr.y, 0);
|
|
19
|
+
const sumXSquare = this.values.reduce((acc, curr) => acc + Math.pow(curr.x, 2), 0);
|
|
20
|
+
const denominator = length * sumXSquare - sumX * sumX;
|
|
21
|
+
if (denominator === 0) {
|
|
22
|
+
throw new Error('The input data points are collinear or insufficient for regression.');
|
|
23
|
+
}
|
|
24
|
+
this._slope = (length * sumXY - sumX * sumY) / denominator;
|
|
25
|
+
this._intercept = (sumY * sumXSquare - sumX * sumXY) / denominator;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
predict(x) {
|
|
29
|
+
if (this._slope === null || this._intercept === null) {
|
|
30
|
+
throw new Error('You need to calculate the regression before making predictions.');
|
|
31
|
+
}
|
|
32
|
+
return this._slope * x + this._intercept;
|
|
33
|
+
}
|
|
34
|
+
get slope() {
|
|
35
|
+
return this._slope;
|
|
36
|
+
}
|
|
37
|
+
get intercept() {
|
|
38
|
+
return this._intercept;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.LinearRegression = LinearRegression;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TwoSum = void 0;
|
|
4
|
+
class TwoSum {
|
|
5
|
+
constructor(numbers, target) {
|
|
6
|
+
this.numbers = numbers;
|
|
7
|
+
this.target = target;
|
|
8
|
+
}
|
|
9
|
+
execute() {
|
|
10
|
+
let left = 0;
|
|
11
|
+
let right = this.numbers.length - 1;
|
|
12
|
+
while (left < right) {
|
|
13
|
+
const sum = this.numbers[left] + this.numbers[right];
|
|
14
|
+
if (sum === this.target) {
|
|
15
|
+
return [left, right];
|
|
16
|
+
}
|
|
17
|
+
else if (sum < this.target) {
|
|
18
|
+
left++;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
right--;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.TwoSum = TwoSum;
|
package/dist/Sort/BubbleSort.js
CHANGED
|
@@ -2,16 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BubbleSort = void 0;
|
|
4
4
|
class BubbleSort {
|
|
5
|
-
constructor() {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
constructor(numbers) {
|
|
6
|
+
this.numbers = numbers;
|
|
7
|
+
return this;
|
|
8
|
+
}
|
|
9
|
+
execute() {
|
|
10
|
+
for (let i = 0; i < this.numbers.length - 1; i++) {
|
|
11
|
+
for (let j = 0; j < this.numbers.length - i - 1; j++) {
|
|
12
|
+
if (this.numbers[j] > this.numbers[j + 1]) {
|
|
13
|
+
[this.numbers[j], this.numbers[j + 1]] = [this.numbers[j + 1], this.numbers[j]];
|
|
11
14
|
}
|
|
12
15
|
}
|
|
13
16
|
}
|
|
14
|
-
return numbers;
|
|
17
|
+
return [...this.numbers];
|
|
15
18
|
}
|
|
16
19
|
}
|
|
17
20
|
exports.BubbleSort = BubbleSort;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SelectionSort = void 0;
|
|
4
|
+
class SelectionSort {
|
|
5
|
+
constructor(numbers) {
|
|
6
|
+
this.numbers = numbers;
|
|
7
|
+
return this;
|
|
8
|
+
}
|
|
9
|
+
execute() {
|
|
10
|
+
for (let i = 0; i < this.numbers.length - 1; i++) {
|
|
11
|
+
let minIndex = i;
|
|
12
|
+
for (let j = i + 1; j < this.numbers.length; j++) {
|
|
13
|
+
if (this.numbers[j] < this.numbers[minIndex]) {
|
|
14
|
+
minIndex = j;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
[this.numbers[i], this.numbers[minIndex]] = [this.numbers[minIndex], this.numbers[i]];
|
|
18
|
+
}
|
|
19
|
+
return [...this.numbers];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.SelectionSort = SelectionSort;
|
package/dist/index.js
CHANGED
|
@@ -14,4 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./Math/Fibonacci"), exports);
|
|
17
18
|
__exportStar(require("./Sort/BubbleSort"), exports);
|
|
19
|
+
__exportStar(require("./Sort/SelectionSort"), exports);
|