Skip to content

Commit a254b86

Browse files
Merge branch 'TheAlgorithms:master' into master
2 parents c58f28f + b783612 commit a254b86

File tree

8 files changed

+146
-21
lines changed

8 files changed

+146
-21
lines changed

Conversions/LengthConversion.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Converts a length from one unit to another.
3+
*
4+
* @param {number} length - The length to convert.
5+
* @param {string} fromUnit - The unit to convert from (e.g., "km", "m", "cm").
6+
* @param {string} toUnit - The unit to convert to (e.g., "km", "m", "cm").
7+
* @returns {number} The converted length.
8+
* @throws {Error} If the units are invalid or not found in the conversion dictionary.
9+
*/
10+
11+
const lengthConversion = (length, fromUnit, toUnit) => {
12+
// Define a dictionary to map units to meters
13+
const meters = {
14+
mm: 0.001,
15+
cm: 0.01,
16+
m: 1,
17+
km: 1000,
18+
inch: 0.0254,
19+
ft: 0.3048,
20+
yd: 0.9144,
21+
mi: 1609.34
22+
}
23+
24+
// Check if the units are in the dictionary, otherwise, throw an error
25+
if (!(fromUnit in meters) || !(toUnit in meters)) {
26+
throw new Error('Invalid units')
27+
}
28+
29+
// Perform the conversion
30+
const metersFrom = length * meters[fromUnit]
31+
const convertedLength = metersFrom / meters[toUnit]
32+
33+
return convertedLength
34+
}
35+
36+
export {
37+
lengthConversion
38+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { lengthConversion } from '../LengthConversion.js'
2+
3+
describe('LengthConversion', () => {
4+
it.each`
5+
length | fromUnit | toUnit | expected
6+
${10} | ${'km'} | ${'m'} | ${10000}
7+
${100} | ${'m'} | ${'km'} | ${0.1}
8+
${5} | ${'cm'} | ${'mm'} | ${50}
9+
${12} | ${'ft'} | ${'inch'}| ${144.00000000000003}
10+
`(
11+
'converts $length $fromUnit to $toUnit',
12+
({ length, fromUnit, toUnit, expected }) => {
13+
try {
14+
const result = lengthConversion(length, fromUnit, toUnit)
15+
expect(result).toBe(expected)
16+
} catch (error) {
17+
expect(error).toBeUndefined()
18+
}
19+
}
20+
)
21+
22+
it.each`
23+
length | fromUnit | toUnit | expected
24+
${10} | ${'m'} | ${'km'} | ${0.01}
25+
${1000}| ${'mm'} | ${'cm'} | ${100}
26+
${1} | ${'inch'}| ${'ft'} | ${0.08333333333}
27+
`(
28+
'converts $length $fromUnit to $toUnit (vice versa)',
29+
({ length, fromUnit, toUnit, expected }) => {
30+
try {
31+
const result = lengthConversion(length, fromUnit, toUnit)
32+
expect(result).toBeCloseTo(expected, 10) // Close comparison due to floating-point precision
33+
} catch (error) {
34+
expect(error).toBeUndefined()
35+
}
36+
}
37+
)
38+
39+
it.each`
40+
length | fromUnit | toUnit | expectedError
41+
${10} | ${'km'} | ${'invalid'} | ${'Invalid units'}
42+
${5} | ${'invalid'} | ${'m'} | ${'Invalid units'}
43+
`(
44+
'returns error message for invalid units: $fromUnit to $toUnit',
45+
({ length, fromUnit, toUnit, expectedError }) => {
46+
try {
47+
lengthConversion(length, fromUnit, toUnit)
48+
} catch (error) {
49+
expect(error.message).toBe(expectedError)
50+
}
51+
}
52+
)
53+
})

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
* [SumOfSubset](Backtracking/SumOfSubset.js)
1010
* **Bit-Manipulation**
1111
* [BinaryCountSetBits](Bit-Manipulation/BinaryCountSetBits.js)
12+
* [IsPowerofFour](Bit-Manipulation/IsPowerofFour.js)
1213
* [IsPowerOfTwo](Bit-Manipulation/IsPowerOfTwo.js)
1314
* [LogTwo](Bit-Manipulation/LogTwo.js)
1415
* [NextPowerOfTwo](Bit-Manipulation/NextPowerOfTwo.js)
1516
* [SetBit](Bit-Manipulation/SetBit.js)
17+
* [UniqueElementInAnArray](Bit-Manipulation/UniqueElementInAnArray.js)
1618
* **Cache**
1719
* [LFUCache](Cache/LFUCache.js)
1820
* [LRUCache](Cache/LRUCache.js)
@@ -270,6 +272,7 @@
270272
* [Problem017](Project-Euler/Problem017.js)
271273
* [Problem018](Project-Euler/Problem018.js)
272274
* [Problem020](Project-Euler/Problem020.js)
275+
* [Problem021](Project-Euler/Problem021.js)
273276
* [Problem023](Project-Euler/Problem023.js)
274277
* [Problem025](Project-Euler/Problem025.js)
275278
* [Problem028](Project-Euler/Problem028.js)

Maths/QuadraticRoots.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @see https://www.cuemath.com/algebra/roots-of-quadratic-equation/
3+
* @author Dibya Debayan Dash
4+
* Calculates the roots of a quadratic equation of the form ax^2 + bx + c = 0.
5+
*
6+
* @param {number} a - Coefficient of x^2.
7+
* @param {number} b - Coefficient of x.
8+
* @param {number} c - Constant term.
9+
* @returns {number[]} - An array containing the roots if they are real,
10+
* or an empty array indicating no real roots.
11+
*
12+
* @example
13+
* // Find the roots of the quadratic equation: 2x^2 - 4x + 2 = 0
14+
* const roots = quadraticRoots(2, -4, 2);
15+
* // Expected output: [1]
16+
*/
17+
const quadraticRoots = (a, b, c) => {
18+
// Calculate the discriminant
19+
const discriminant = b * b - 4 * a * c
20+
21+
// Check if roots are real
22+
if (discriminant < 0) {
23+
return []
24+
} else if (discriminant === 0) {
25+
// One real root
26+
return [-b / (2 * a)]
27+
} else {
28+
// Two real roots
29+
const sqrtDiscriminant = Math.sqrt(discriminant)
30+
return [
31+
(-b + sqrtDiscriminant) / (2 * a),
32+
(-b - sqrtDiscriminant) / (2 * a)
33+
]
34+
}
35+
}
36+
37+
export { quadraticRoots }

Maths/test/ParityOutlier.test.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ describe('Testing parityOutlier function', () => {
99
expect(parityOutlier([177, 5, 76, 1919])).toBe(76)
1010
})
1111

12-
it('should, if the given array has only one integer element, return the integer itself', () => {
13-
expect(parityOutlier([83])).toBe(83)
14-
expect(parityOutlier([54])).toBe(54)
15-
})
16-
1712
it('should, if the given array has only an odd and an even number, return the odd outlier', () => {
1813
expect(parityOutlier([1, 2])).toBe(1)
1914
expect(parityOutlier([4, 3])).toBe(3)

Maths/test/QuadraticRoots.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { quadraticRoots } from '../QuadraticRoots.js'
2+
3+
describe('quadratic roots', () => {
4+
it('returns an array with two real roots when the discriminant is positive', () => {
5+
expect(quadraticRoots(1, -3, 2)).toEqual([2, 1])
6+
})
7+
it('returns an array with one real root when the discriminant is zero', () => {
8+
expect(quadraticRoots(1, -2, 1)).toEqual([1])
9+
})
10+
it('returns an empty array indicating no real roots when the discriminant is negative', () => {
11+
expect(quadraticRoots(1, 2, 5)).toEqual([])
12+
})
13+
})

Project-Euler/test/Problem044.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ describe('checking nth prime number', () => {
1212
expect(problem44(1)).toBe(5482660)
1313
})
1414
// Project Euler Second Value for Condition Check
15-
// FIXME skip this test for now because it runs very long and clogs up the CI & pre-commit hook
16-
test('if the number is greater or equal to 2167', () => {
15+
// Skipping this by default as it makes CI runs take way too long
16+
test.skip('if the number is greater or equal to 2167', () => {
1717
expect(problem44(2167)).toBe(8476206790)
1818
})
1919
})

String/test/ValidateUrl.test.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)