Skip to content
Open
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
17 changes: 10 additions & 7 deletions Data-Structures/Tree/BinarySearchTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ const Node = (function Node() {
visit(output = (value) => console.log(value)) {
// Recursively go left
if (this.left !== null) {
this.left.visit()
this.left.visit(output)
}
// Print out value
output(this.value)
// Recursively go right
if (this.right !== null) {
this.right.visit()
this.right.visit(output)
}
}

Expand Down Expand Up @@ -116,20 +116,23 @@ const Tree = (function () {
}

// Inorder traversal
traverse() {
traverse(output = (value) => console.log(value)) {
if (!this.root) {
// No nodes are there in the tree till now
return
}
this.root.visit()
this.root.visit(output)
}

// Start by searching the root
search(val) {
const found = this.root.search(val)
if (found !== null) {
return found.value
if (this.root) {
const found = this.root.search(val)
if (found !== null) {
return found.value
}
}

// not found
return null
}
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
66 changes: 66 additions & 0 deletions Data-Structures/Tree/test/BinarySearchTree.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Tree } from '../BinarySearchTree.js'

describe('Binary Search Tree', () => {
let tree

beforeEach(() => {
tree = new Tree()
tree.addValue(10)
tree.addValue(5)
tree.addValue(15)
tree.addValue(3)
tree.addValue(8)
})

test('should add values to the tree', () => {
tree.addValue(12)

expect(tree.search(12)).toBe(12)
expect(tree.search(5)).toBe(5)
expect(tree.search(15)).toBe(15)
})

test('should perform in-order traversal', () => {
const values = []
const output = (val) => values.push(val)
tree.traverse(output)
expect(values).toEqual([3, 5, 8, 10, 15])
})

test('should remove leaf nodes correctly', () => {
tree.removeValue(5)
expect(tree.search(5)).toBeNull()
})

test('should remove nodes with one child correctly', () => {
tree.addValue(12)
tree.removeValue(15)

expect(tree.search(15)).toBeNull()
expect(tree.search(12)).toBe(12)
})

test('should remove nodes with two children correctly', () => {
tree.addValue(18)
tree.removeValue(15)

expect(tree.search(15)).toBeNull()
expect(tree.search(18)).toBe(18)
})

test('should return null for non-existent values', () => {
expect(tree.search(20)).toBeNull()
expect(tree.search(0)).toBeNull()
})

test('should handle removal of root node correctly', () => {
tree.removeValue(10)
expect(tree.search(10)).toBeNull()
})

test('should handle empty tree gracefully', () => {
const newTree = new Tree()
newTree.removeValue(22) // Should not throw
expect(newTree.search(22)).toBeNull()
})
})
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
Loading