10000 Merge branch 'TheAlgorithms:master' into feat/evaluate-postfix-string · TheAlgorithms/JavaScript@d45ef6d · GitHub
[go: up one dir, main page]

Skip to content

Commit d45ef6d

Browse files
authored
Merge branch 'TheAlgorithms:master' into feat/evaluate-postfix-string
2 parents 5105fe9 + f8ffacd commit d45ef6d

File tree

7 files changed

+118
-12
lines changed

7 files changed

+118
-12
lines changed

Backtracking/GeneratePermutations.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Problem Statement: Generate all distinct permutations of a an array (all permutations should be in sorted order);
2+
* Problem Statement: Generate all distinct permutations of an array (all permutations should be in sorted order);
33
*
44
* What is permutations?
55
* - Permutation means possible arrangements in a set (here it is an array);

Bit-Manipulation/GrayCodes.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Generates a Gray code sequence for the given number of bits.
3+
* @param {number} n - The number of bits in the Gray code sequence.
4+
* @returns {number[]} - An array of Gray codes in binary format.
5+
* @description
6+
* Gray codes are binary sequences in which two successive values differ in only one bit.
7+
* This function generates a Gray code sequence of length 2^n for the given number of bits.
8+
*
9+
* The algorithm follows these steps:
10+
*
11+
* 1. Initialize an array `grayCodes` to store the Gray codes. Start with [0, 1] for n = 1.
12+
* 2. Iterate from 1 to n:
13+
* a. Calculate `highestBit` as 2^i, where `i` is the current iteration index.
14+
* b. Iterate in reverse order through the existing Gray codes:
15+
* - For each Gray code `code`, add `highestBit | code` to `grayCodes`.
16+
* - This operation flips a single bit in each existing code, creating new codes.
17+
* 3. Return the `grayCodes` array containing the Gray codes in decimal representation.
18+
*
19+
*resources: [GFG](https://www.geeksforgeeks.org/generate-n-bit-gray-codes/)
20+
* @example
21+
* const n = 3;
22+
* const grayCodes = generateGrayCodes(n);
23+
* // grayCodes will be [0, 1, 3, 2, 6, 7, 5, 4] for n=3.
24+
*/
25+
function generateGrayCodes(n) {
26+
if (n <= 0) {
27+
return [0]
28+
}
29+
30+
const grayCodes = [0, 1]
31+
32+
for (let i = 1; i < n; i++) {
33+
const highestBit = 1 << i
34+
for (let j = grayCodes.length - 1; j >= 0; j--) {
35+
grayCodes.push(highestBit | grayCodes[j])
36+
}
37+
}
38+
39+
return grayCodes
40+
}
41+
42+
export { generateGrayCodes }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { generateGrayCodes } from '../GrayCodes.js'
2+
3+
describe('Gray codes', () => {
4+
test.each([
5+
[0, [0b0]],
6+
[1, [0b0, 0b1]],
7+
[2, [0b00, 0b01, 0b11, 0b10]],
8+
[3, [0b000, 0b001, 0b011, 0b010, 0b110, 0b111, 0b101, 0b100]],
9+
[
10+
4,
11+
[
12+
0b0000, 0b0001, 0b0011, 0b0010, 0b0110, 0b0111, 0b0101, 0b0100, 0b1100,
13+
0b1101, 0b1111, 0b1110, 0b1010, 0b1011, 0b1001, 0b1000
14+
]
15+
]
16+
])('n = %i -> %j', (n, expected) => {
17+
expect(generateGrayCodes(n)).toEqual(expected)
18+
})
19+
})

Maths/EuclideanDistance.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @see [Wikipedia](https://en.wikipedia.org/wiki/Euclidean_distance)
3+
* Calculate the Euclidean distance between two vectors.
4+
* @param {number[]} vector1 - The first vector.
5+
* @param {number[]} vector2 - The second vector.
6+
* @returns {number} The Euclidean distance between the two vectors.
7+
*/
8+
9+
const EuclideanDistance = (vector1, vector2) => {
10+
let sumOfSquares = 0
11+
12+
for (let i = 0; i < vector1.length; i++) {
13+
sumOfSquares += Math.pow(vector1[i] - vector2[i], 2)
14+
}
15+
16+
return Math.sqrt(sumOfSquares)
17+
}
18+
19+
export { EuclideanDistance }

Maths/test/EuclideanDistance.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { EuclideanDistance } from '../EuclideanDistance.js'
2+
3+
describe('EuclideanDistance', () => {
4+
it('should calculate the distance correctly for 2D vectors', () => {
5+
expect(EuclideanDistance([0, 0], [2, 2])).toBeCloseTo(2.8284271247461903, 10)
6+
})
7+
8+
it('should calculate the distance correctly for 3D vectors', () => {
9+
expect(EuclideanDistance([0, 0, 0], [2, 2, 2])).toBeCloseTo(3.4641016151377544, 10)
10+
})
11+
12+
it('should calculate the distance correctly for 4D vectors', () => {
13+
expect(EuclideanDistance([1, 2, 3, 4], [5, 6, 7, 8])).toBeCloseTo(8.0, 10)
14+
})
15+
16+
it('should calculate the distance correctly for different 2D vectors', () => {
17+
expect(EuclideanDistance([1, 2], [4, 6])).toBeCloseTo(5.0, 10)
18+
})
19+
})

Recursive/Factorial.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@
99
*/
1010

1111
const factorial = (n) => {
12-
if (!Number.isInteger(n)) {
13-
throw new RangeError('Not a Whole Number')
14-
}
15-
16-
if (n < 0) {
17-
throw new RangeError('Not a Positive Number')
12+
if (!Number.isInteger(n) || n < 0) {
13+
throw new RangeError('Input should be a non-negative whole number')
1814
}
1915

2016
if (n === 0) {
2117
return 1
2218
}
19+
2320
return n * factorial(n - 1)
2421
}
2522

Recursive/test/Factorial.test.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@ describe('Factorial', () => {
1010
})
1111

1212
it('Throw Error for Invalid Input', () => {
13-
expect(() => factorial('-')).toThrow('Not a Whole Number')
14-
expect(() => factorial(null)).toThrow('Not a Whole Number')
15-
expect(() => factorial(undefined)).toThrow('Not a Whole Number')
16-
expect(() => factorial(3.142)).toThrow('Not a Whole Number')
17-
expect(() => factorial(-1)).toThrow('Not a Positive Number')
13+
expect(() => factorial('-')).toThrow(
14+
'Input should be a non-negative whole number'
15+
)
16+
expect(() => factorial(null)).toThrow(
17+
'Input should be a non-negative whole number'
18+
)
19+
expect(() => factorial(undefined)).toThrow(
20+
'Input should be a non-negative whole number'
21+
)
22+
expect(() => factorial(3.142)).toThrow(
23+
'Input should be a non-negative whole number'
24+
)
25+
expect(() => factorial(-1)).toThrow(
26+
'Input should be a non-negative whole number'
27+
)
1828
})
1929
})

0 commit comments

Comments
 (0)
0