Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/Ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
with:
# file types to ignore
skip: "*.json,*.yml,DIRECTORY.md"
ignore_words_list: "ba,esy,yse,falsy"
ignore_words_list: "ba,esy,yse,falsy,abd"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ yarn-error.log*

# intelliJ workspace folder
.idea
.vscode
.trae

/coverage
2 changes: 1 addition & 1 deletion Ciphers/KeyFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function keyFinder(str) {
return k // return the key number if founded
}
outStrElement = '' // reset the temp word
} // end for ( let i=0; i < wordBank.length; i++)
} // end for (let i=0; i < wordBank.length; i++)
}
}
return 0 // return 0 if found nothing
Expand Down
5 changes: 4 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
* [UpperCaseConversion](Conversions/UpperCaseConversion.js)
* **Data-Structures**
* **Array**
* [LocalMaximomPoint](Data-Structures/Array/LocalMaximomPoint.js)
* [LocalMaximumPoint](Data-Structures/Array/LocalMaximumPoint.js)
* [NumberOfLocalMaximumPoints](Data-Structures/Array/NumberOfLocalMaximumPoints.js)
* [QuickSelect](Data-Structures/Array/QuickSelect.js)
* [Reverse](Data-Structures/Array/Reverse.js)
Expand Down Expand Up @@ -321,6 +321,9 @@
* [StringSearch](Search/StringSearch.js)
* [TernarySearch](Search/TernarySearch.js)
* [UnionFind](Search/UnionFind.js)
* **Sliding-Windows**
* [MaxSumSubarrayFixed](Sliding-Windows/MaxSumSubarrayFixed.js)
* [LongestSubarrayWithSumAtMost](Sliding-Windows/LongestSubarrayWithSumAtMost.js)
* **Sorts**
* [AlphaNumericalSort](Sorts/AlphaNumericalSort.js)
* [BeadSort](Sorts/BeadSort.js)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ const findMaxPointIndex = (
}
}

const LocalMaximomPoint = (A) => findMaxPointIndex(A, 0, A.length - 1, A.length)
const LocalMaximumPoint = (A) => findMaxPointIndex(A, 0, A.length - 1, A.length)

export { LocalMaximomPoint }
export { LocalMaximumPoint }
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { LocalMaximomPoint } from '../LocalMaximomPoint'
import { LocalMaximumPoint } from '../LocalMaximumPoint'

describe('LocalMaximumPoint tests', () => {
it('test boundary maximum points - last element', () => {
const Array = [1, 2, 3, 4, 5, 6, 12]
expect(LocalMaximomPoint(Array)).toEqual(6)
expect(LocalMaximumPoint(Array)).toEqual(6)
})

it('test boundary maximum points - first element', () => {
const Array2 = [13, 6, 5, 4, 3, 2, 1]
expect(LocalMaximomPoint(Array2)).toEqual(0)
expect(LocalMaximumPoint(Array2)).toEqual(0)
})

it('test boundary maximum points - should find first maximom point from the top', () => {
it('test boundary maximum points - should find first maximum point from the top', () => {
// Test a mix of number types (i.e., positive/negative, numbers with decimals, fractions)
const Array = [13, 2, 3, 4, 5, 6, 12]
expect(LocalMaximomPoint(Array)).toEqual(6)
expect(LocalMaximumPoint(Array)).toEqual(6)
})

it('test inner points - second element', () => {
const Array2 = [13, 16, 5, 4, 3, 2, 1]
expect(LocalMaximomPoint(Array2)).toEqual(1)
expect(LocalMaximumPoint(Array2)).toEqual(1)
})

it('test inner points - element some where in the middle', () => {
const Array2 = [13, 16, 5, 41, 3, 2, 1]
expect(LocalMaximomPoint(Array2)).toEqual(3)
expect(LocalMaximumPoint(Array2)).toEqual(3)
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NumberOfLocalMaximumPoints } from '../NumberOfLocalMaximumPoints'

describe('LocalMaximomPoint tests', () => {
describe('LocalMaximumPoint tests', () => {
it('test boundary maximum points - last element', () => {
const Array = [1, 2, 3, 4, 5, 6, 12]
expect(NumberOfLocalMaximumPoints(Array)).toEqual(1)
Expand Down
2 changes: 1 addition & 1 deletion Data-Structures/Tree/SegmentTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SegmentTree {
}
}

// interval [L,R) with left index(L) included and right (R) excluded.
// interval [L, R) with left index(L) included and right (R) excluded.
query(left, right) {
const { size, tree } = this
// cause R is excluded, increase right for convenient
Expand Down
2 changes: 1 addition & 1 deletion Dynamic-Programming/UniquePaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const uniquePaths = (m, n) => {
for (let j = 1; j < m; j++) {
// paths[j] in RHS represents the cell value stored above the current cell
// paths[j-1] in RHS represents the cell value stored to the left of the current cell
// paths [j] on the LHS represents the number of distinct pathways to the cell (i,j)
// paths [j] on the LHS represents the number of distinct pathways to the cell (i, j)
paths[j] = paths[j - 1] + paths[j]
}
}
Expand Down
2 changes: 1 addition & 1 deletion Geometry/ConvexHullGraham.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function compare(a, b) {
return 1
}
function orientation(a, b, c) {
// Check orientation of Line(a,b) and Line(b,c)
// Check orientation of Line(a, b) and Line(b, c)
const alpha = (b.y - a.y) / (b.x - a.x)
const beta = (c.y - b.y) / (c.x - b.x)

Expand Down
2 changes: 1 addition & 1 deletion Graphs/Dijkstra.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

function createGraph(V, E) {
// V - Number of vertices in graph
// E - Number of edges in graph (u,v,w)
// E - Number of edges in graph (u, v, w)
const adjList = [] // Adjacency list
for (let i = 0; i < V; i++) {
adjList.push([])
Expand Down
2 changes: 1 addition & 1 deletion Maths/Abs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const abs = (num) => {
throw new TypeError('Argument is NaN - Not a Number')
}

return validNumber < 0 ? -validNumber : validNumber // if number is less than zero mean negative then it converted to positive. i.e -> n = -2 = -(-2) = 2
return validNumber < 0 ? -validNumber : validNumber // if number is less than zero means negative, then it converted to positive. i.e., n = -2 = -(-2) = 2
}

export { abs }
23 changes: 17 additions & 6 deletions Maths/AverageMean.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
/**
* @function mean
* @description This script will find the mean value of a array of numbers.
* @param {Integer[]} nums - Array of integer
* @return {Integer} - mean of nums.
* @param {number[]} numbers - Array of integer
* @return {number} - mean of numbers.
* @throws {TypeError} If the input is not an array or contains non-number elements.
* @throws {Error} If the input array is empty.
* @see [Mean](https://en.wikipedia.org/wiki/Mean)
* @example mean([1, 2, 4, 5]) = 3
* @example mean([10, 40, 100, 20]) = 42.5
*/

const mean = (nums) => {
if (!Array.isArray(nums)) {
const mean = (numbers) => {
if (!Array.isArray(numbers)) {
throw new TypeError('Invalid Input')
} else if (numbers.length === 0) {
throw new Error('Array is empty')
}

return nums.reduce((sum, cur) => sum + cur, 0) / nums.length
let total = 0
numbers.forEach((num) => {
if (typeof num !== 'number') {
throw new TypeError('Invalid Input')
}
total += num
})

return total / numbers.length
}

export { mean }
2 changes: 1 addition & 1 deletion Maths/BinaryExponentiationIterative.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// To calculate x^n i.e. exponent(x, n) in O(log n) time in iterative way
// To calculate x^n i.e., exponent(x, n) in O(log n) time in iterative way
// n is an integer and n >= 0

// Explanation: https://en.wikipedia.org/wiki/Exponentiation_by_squaring
Expand Down
2 changes: 1 addition & 1 deletion Maths/EulersTotient.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const EulersTotient = (n) => {
while (n % i === 0) {
n = Math.floor(n / i)
}
// i is a prime diving n, multiply res by 1 - 1/i
// i is a prime dividing n, multiply res by 1 - 1/i
// res = res * (1 - 1/i) = res - (res / i)
res = res - Math.floor(res / i)
}
Expand Down
2 changes: 1 addition & 1 deletion Maths/EulersTotientFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const gcdOfTwoNumbers = (x, y) => {
// let gcd of x and y is gcdXY
// so it divides x and y completely
// so gcdXY should also divide y%x (y = gcdXY*a and x = gcdXY*b and y%x = y - x*k so y%x = gcdXY(a - b*k))
// and gcd(x,y) is equal to gcd(y%x , x)
// and gcd(x,y) is equal to gcd(y%x, x)
return x === 0 ? y : gcdOfTwoNumbers(y % x, x)
}

Expand Down
2 changes: 1 addition & 1 deletion Maths/MatrixMultiplication.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const matrixCheck = (matrix) => {
}
}

// tests to see if the matrices have a like side, i.e. the row length on the first matrix matches the column length on the second matrix, or vice versa.
// tests to see if the matrices have a like side, i.e., the row length on the first matrix matches the column length on the second matrix, or vice versa.
const twoMatricesCheck = (first, second) => {
const [firstRowLength, secondRowLength, firstColLength, secondColLength] = [
first.length,
Expand Down
2 changes: 1 addition & 1 deletion Maths/Polynomial.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Polynomial {
constructor(array) {
this.coefficientArray = array // array of coefficients
this.polynomial = '' // in terms of x e.g. (2x) + (1)
this.polynomial = '' // in terms of x e.g., (2x) + (1)
this.construct()
}

Expand Down
46 changes: 46 additions & 0 deletions Maths/test/AverageMean.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,50 @@ describe('Tests for average mean', () => {
const meanFunction = mean([10, 40, 100, 20])
expect(meanFunction).toBe(42.5)
})

it('should return the number itself for a single-element array', () => {
const result = mean([5])
expect(result).toBe(5)
})

it('should throw error for empty array', () => {
expect(() => mean([])).toThrow()
})

it('should throw error for an array containing null', () => {
expect(() => mean([1, 2, null])).toThrow()
})

it('should throw error for an array containing booleans', () => {
expect(() => mean([1, 2, true])).toThrow()
})

it('should throw error for an array containing strings', () => {
expect(() => mean([1, 2, 'asd'])).toThrow()
})

it('should return the mean of an array with negative numbers', () => {
const result = mean([-1, -2, -3, -4])
expect(result).toBe(-2.5)
})

it('should return the mean of an array with floating-point numbers', () => {
const result = mean([1.5, 2.5, 3.5])
expect(result).toBe(2.5)
})

it('should return 0 for an array with zeros', () => {
const result = mean([0, 0, 0])
expect(result).toBe(0)
})

it('should handle very large numbers correctly', () => {
const result = mean([1000000000, 2000000000, 3000000000])
expect(result).toBe(2000000000)
})

it('should return correct mean for an array with integers and floating-point numbers', () => {
const result = mean([1, 2.5, 3])
expect(result).toBeCloseTo(2.1667, 4)
})
})
2 changes: 1 addition & 1 deletion Maths/test/Pow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ describe('Testing powFaster function', () => {
})

it('should return the result in O(lonN) complexity', () => {
expect(powFaster(2, 64)).toBe(18446744073709552000) // execution time Math.log2(64) -> 6
expect(powFaster(2, 64)).toBe(18446744073709552000) // execution time Math. log2(64) -> 6
})
})
2 changes: 1 addition & 1 deletion Search/UnionFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function UnionFind(n, key) {
}
let cnt, length
// init Union Find with number of distinct groups. Each group will be referred to as index of the array of size 'size' starting at 0.
// Provide an optional key function that maps these indices. I.e. for the groups starting with 1 provide function(a){return a-1;}. The default value is function(a){return a;}.
// Provide an optional key function that maps these indices. I.e., for the groups starting with 1 provide function(a){return a-1;}. The default value is function(a){return a;}.
key =
key ||
function (a) {
Expand Down
21 changes: 21 additions & 0 deletions Sliding-Windows/LongestSubarrayWithSumAtMost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Function to find the longest subarray with a sum <= target.
*
* @param {number[]} arr - The input array of numbers.
* @param {number} target - The target sum for the dynamic window.
* @returns {number} - The length of the longest subarray with a sum <= target.
*/
export function longestSubarrayWithSumAtMost(arr, target) {
let maxLength = 0
let windowSum = 0
let left = 0
for (let right = 0; right < arr.length; right++) {
windowSum += arr[right]
while (windowSum > target) {
windowSum -= arr[left]
left++
}
maxLength = Math.max(maxLength, right - left + 1)
}
return maxLength
}
26 changes: 26 additions & 0 deletions Sliding-Windows/MaxSumSubarrayFixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Function to find the maximum sum of a subarray of fixed size k.
*
* @param {number[]} arr - The input array of numbers.
* @param {number} k - The fixed size of the subarray.
* @returns {number} - The maximum sum of any subarray of size k.
* @throws {RangeError} - If k is larger than the array length or less than 1.
*/
export function maxSumSubarrayFixed(arr, k) {
if (k > arr.length || k < 1) {
throw new RangeError(
'Subarray size k must be between 1 and the length of the array'
)
}
let maxSum = 0
let windowSum = 0
for (let i = 0; i < k; i++) {
windowSum += arr[i]
}
maxSum = windowSum
for (let i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k]
maxSum = Math.max(maxSum, windowSum)
}
return maxSum
}
24 changes: 24 additions & 0 deletions Sliding-Windows/test/LongestSubarrayWithSumAtMost.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { longestSubarrayWithSumAtMost } from '../LongestSubarrayWithSumAtMost'

describe('Dynamic-size Sliding Window - longestSubarrayWithSumAtMost', () => {
it('should return the longest subarray length with sum <= target', () => {
const arr = [1, 2, 3, 4, 5]
const target = 7
const result = longestSubarrayWithSumAtMost(arr, target)
expect(result).toBe(3)
})

it('should return the full array length if the entire sum is within the target', () => {
const arr = [1, 1, 1, 1]
const target = 4
const result = longestSubarrayWithSumAtMost(arr, target)
expect(result).toBe(4)
})

it('should return 0 if no subarray meets the sum condition', () => {
const arr = [5, 6, 7]
const target = 3
const result = longestSubarrayWithSumAtMost(arr, target)
expect(result).toBe(0)
})
})
22 changes: 22 additions & 0 deletions Sliding-Windows/test/MaxSumSubarrayFixed.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { maxSumSubarrayFixed } from '../MaxSumSubarrayFixed'

describe('Fixed-size Sliding Window - maxSumSubarrayFixed', () => {
it('should return the maximum sum of a subarray of size k', () => {
const arr = [2, 1, 5, 1, 3, 2]
const k = 3
const result = maxSumSubarrayFixed(arr, k)
expect(result).toBe(9)
})

it('should throw a RangeError if k is larger than the array length', () => {
const arr = [2, 1, 5]
const k = 4
expect(() => maxSumSubarrayFixed(arr, k)).toThrow(RangeError)
})

it('should throw a RangeError if k is less than 1', () => {
const arr = [2, 1, 5]
const k = 0
expect(() => maxSumSubarrayFixed(arr, k)).toThrow(RangeError)
})
})
2 changes: 1 addition & 1 deletion String/AlphaNumericPalindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* like "2A3*3a2", "2A3 3a2", and "2_A3*3#A2"
*
* But the catch is, we have to check only if the alphanumeric characters
* are palindrome i.e remove spaces, symbols, punctuations etc
* are palindrome i.e remove spaces, symbols, punctuation etc
* and the case of the characters doesn't matter
*/
const alphaNumericPalindrome = (str) => {
Expand Down
2 changes: 1 addition & 1 deletion String/CheckPalindrome.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Palindrome check is case sensitive; i.e. Aba is not a palindrome
// Palindrome check is case sensitive; i.e., Aba is not a palindrome
// input is a string
const checkPalindrome = (str) => {
// check that input is a string
Expand Down