Compare commits

...

5 Commits

Author SHA1 Message Date
Kachhadiyn
76d8ad6fbe
Merge 45762593b5 into 2c67b48c21 2024-04-25 08:24:51 +08:00
=
45762593b5 Added Linear Search Fix 2020-10-17 20:30:01 +05:30
=
ceed279de8 Remove Array check and optimise 2020-10-17 20:16:03 +05:30
=
b5ca4b12e8 Added Mini-Max sum Algorithm 2020-10-17 20:06:20 +05:30
=
677146717d Typo Fix in BFS test 2020-10-17 19:54:34 +05:30
7 changed files with 77 additions and 27 deletions

View File

@ -92,6 +92,7 @@ a set of rules that precisely define a sequence of operations.
* `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` [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` [Horner's method](src/algorithms/math/horner-method) - polynomial evaluation
* `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', () => {
const array = [1, 2, 4, 6, 2];
expect(linearSearch(array, 10)).toEqual([]);
expect(linearSearch(array, 1)).toEqual([0]);
expect(linearSearch(array, 2)).toEqual([1, 4]);
expect(linearSearch(array, 10)).toEqual(-1);
expect(linearSearch(array, 1)).toEqual(0);
expect(linearSearch(array, 2)).toEqual(1);
});
it('should search all strings in array', () => {
const array = ['a', 'b', 'a'];
expect(linearSearch(array, 'c')).toEqual([]);
expect(linearSearch(array, 'b')).toEqual([1]);
expect(linearSearch(array, 'a')).toEqual([0, 2]);
expect(linearSearch(array, 'c')).toEqual(-1);
expect(linearSearch(array, 'b')).toEqual(1);
expect(linearSearch(array, 'a')).toEqual(0);
});
it('should search through objects as well', () => {
@ -33,8 +33,8 @@ describe('linearSearch', () => {
{ key: 6 },
];
expect(linearSearch(array, { key: 10 }, comparatorCallback)).toEqual([]);
expect(linearSearch(array, { key: 5 }, comparatorCallback)).toEqual([0]);
expect(linearSearch(array, { key: 6 }, comparatorCallback)).toEqual([1, 3]);
expect(linearSearch(array, { key: 10 }, comparatorCallback)).toEqual(-1);
expect(linearSearch(array, { key: 5 }, comparatorCallback)).toEqual(0);
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) {
const comparator = new Comparator(comparatorCallback);
const foundIndices = [];
array.forEach((element, index) => {
if (comparator.equal(element, seekElement)) {
foundIndices.push(index);
for (let i = 0; i < array.length; i += 1) {
if (comparator.equal(array[i], seekElement)) {
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) {
let currentIndex = i;
const temp = array[currentIndex];
// Call visiting callback.
this.callbacks.visitingCallback(array[i]);
// Check if previous element is greater than current element.
// If so, swap the two elements.
while (
array[currentIndex - 1] !== undefined
&& this.comparator.lessThan(array[currentIndex], array[currentIndex - 1])
currentIndex > 0
&& this.comparator.lessThan(temp, array[currentIndex - 1])
) {
// Call visiting callback.
this.callbacks.visitingCallback(array[currentIndex - 1]);
// Swap the elements.
[
array[currentIndex - 1],
array[currentIndex],
] = [
array[currentIndex],
array[currentIndex - 1],
];
array[currentIndex] = array[currentIndex - 1];
// Shift current index left.
currentIndex -= 1;
}
array[currentIndex] = temp;
}
return array;