This commit is contained in:
Kachhadiyn 2024-07-17 10:39:36 +09:00 committed by GitHub
commit bd37988dc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 77 additions and 27 deletions

View File

@ -95,6 +95,7 @@ a set of rules that precisely define a sequence of operations.
* `B` [Pascal's Triangle](src/algorithms/math/pascal-triangle) * `B` [Pascal's Triangle](src/algorithms/math/pascal-triangle)
* `B` [Complex Number](src/algorithms/math/complex-number) - complex numbers and basic operations with them * `B` [Complex Number](src/algorithms/math/complex-number) - complex numbers and basic operations with them
* `B` [Radian & Degree](src/algorithms/math/radian) - radians to degree and backwards conversion * `B` [Radian & Degree](src/algorithms/math/radian) - radians to degree and backwards conversion
* `B` [Mini-Max Sum](src/algorithms/math/mini-max-sum)
* `B` [Fast Powering](src/algorithms/math/fast-powering) * `B` [Fast Powering](src/algorithms/math/fast-powering)
* `B` [Horner's method](src/algorithms/math/horner-method) - polynomial evaluation * `B` [Horner's method](src/algorithms/math/horner-method) - polynomial evaluation
* `B` [Matrices](src/algorithms/math/matrix) - matrices and basic matrix operations (multiplication, transposition, etc.) * `B` [Matrices](src/algorithms/math/matrix) - matrices and basic matrix operations (multiplication, transposition, etc.)

View File

@ -0,0 +1,27 @@
/**
*
* @param numbers - array of numbers
* @return [number - Max, number - Min]
*/
export default function miniMaxSum(numbers) {
if (!Array.isArray(numbers) || numbers.length === 0) {
return [null, null];
}
let min = numbers[0];
let max = numbers[0];
let sum = numbers[0];
for (let i = 1; i < numbers.length; i += 1) {
sum += numbers[i];
if (numbers[i] > max) {
max = numbers[i];
} else if (numbers[i] < min) {
min = numbers[i];
}
}
return [sum - max, sum - min];
}

View File

@ -0,0 +1,12 @@
# Mini-Max Sum
Given an array of five positive integers, find the minimum and maximum values that
can be calculated by summing exactly four of the array integers. Then return the
respective minimum and maximum values as a array of numbers
```
[1, 2, 3, 4, 5] = [10, 14]
[1, 3, 5, 7, 9] = [16, 24]
```

View File

@ -0,0 +1,13 @@
import miniMaxSum from '../miniMaxSum';
describe('miniMaxSum', () => {
it('should calculate miniMaxSum', () => {
expect(miniMaxSum([1, 3, 5, 7, 9])).toStrictEqual([16, 24]);
expect(miniMaxSum([1, 2, 3, 4, 5])).toStrictEqual([10, 14]);
expect(miniMaxSum([-1, -2, 3, 4, 5])).toStrictEqual([4, 11]);
expect(miniMaxSum([0, 1, 2, 3, 4, 5])).toStrictEqual([10, 15]);
expect(miniMaxSum([])).toStrictEqual([null, null]);
expect(miniMaxSum(12)).toStrictEqual([null, null]);
expect(miniMaxSum(null)).toStrictEqual([null, null]);
});
});

View File

@ -4,17 +4,17 @@ describe('linearSearch', () => {
it('should search all numbers in array', () => { it('should search all numbers in array', () => {
const array = [1, 2, 4, 6, 2]; const array = [1, 2, 4, 6, 2];
expect(linearSearch(array, 10)).toEqual([]); expect(linearSearch(array, 10)).toEqual(-1);
expect(linearSearch(array, 1)).toEqual([0]); expect(linearSearch(array, 1)).toEqual(0);
expect(linearSearch(array, 2)).toEqual([1, 4]); expect(linearSearch(array, 2)).toEqual(1);
}); });
it('should search all strings in array', () => { it('should search all strings in array', () => {
const array = ['a', 'b', 'a']; const array = ['a', 'b', 'a'];
expect(linearSearch(array, 'c')).toEqual([]); expect(linearSearch(array, 'c')).toEqual(-1);
expect(linearSearch(array, 'b')).toEqual([1]); expect(linearSearch(array, 'b')).toEqual(1);
expect(linearSearch(array, 'a')).toEqual([0, 2]); expect(linearSearch(array, 'a')).toEqual(0);
}); });
it('should search through objects as well', () => { it('should search through objects as well', () => {
@ -33,8 +33,8 @@ describe('linearSearch', () => {
{ key: 6 }, { key: 6 },
]; ];
expect(linearSearch(array, { key: 10 }, comparatorCallback)).toEqual([]); expect(linearSearch(array, { key: 10 }, comparatorCallback)).toEqual(-1);
expect(linearSearch(array, { key: 5 }, comparatorCallback)).toEqual([0]); expect(linearSearch(array, { key: 5 }, comparatorCallback)).toEqual(0);
expect(linearSearch(array, { key: 6 }, comparatorCallback)).toEqual([1, 3]); expect(linearSearch(array, { key: 6 }, comparatorCallback)).toEqual(1);
}); });
}); });

View File

@ -10,13 +10,12 @@ import Comparator from '../../../utils/comparator/Comparator';
*/ */
export default function linearSearch(array, seekElement, comparatorCallback) { export default function linearSearch(array, seekElement, comparatorCallback) {
const comparator = new Comparator(comparatorCallback); const comparator = new Comparator(comparatorCallback);
const foundIndices = [];
array.forEach((element, index) => { for (let i = 0; i < array.length; i += 1) {
if (comparator.equal(element, seekElement)) { if (comparator.equal(array[i], seekElement)) {
foundIndices.push(index); return i;
} }
}); }
return foundIndices; return -1;
} }

View File

@ -8,30 +8,28 @@ export default class InsertionSort extends Sort {
for (let i = 1; i < array.length; i += 1) { for (let i = 1; i < array.length; i += 1) {
let currentIndex = i; let currentIndex = i;
const temp = array[currentIndex];
// Call visiting callback. // Call visiting callback.
this.callbacks.visitingCallback(array[i]); this.callbacks.visitingCallback(array[i]);
// Check if previous element is greater than current element. // Check if previous element is greater than current element.
// If so, swap the two elements. // If so, swap the two elements.
while ( while (
array[currentIndex - 1] !== undefined currentIndex > 0
&& this.comparator.lessThan(array[currentIndex], array[currentIndex - 1]) && this.comparator.lessThan(temp, array[currentIndex - 1])
) { ) {
// Call visiting callback. // Call visiting callback.
this.callbacks.visitingCallback(array[currentIndex - 1]); this.callbacks.visitingCallback(array[currentIndex - 1]);
// Swap the elements. // Swap the elements.
[ array[currentIndex] = array[currentIndex - 1];
array[currentIndex - 1],
array[currentIndex],
] = [
array[currentIndex],
array[currentIndex - 1],
];
// Shift current index left. // Shift current index left.
currentIndex -= 1; currentIndex -= 1;
} }
array[currentIndex] = temp;
} }
return array; return array;