Skip to content

Commit ecf1a34

Browse files
authored
Merge branch 'TheAlgorithms:master' into LLupdates
2 parents e0ddd05 + 05e3248 commit ecf1a34

21 files changed

+989
-327
lines changed

Ciphers/KeyFinder.js

Lines changed: 77 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
/******************************************************
2-
Find and retrieve the encryption key automatically
3-
Note: This is a draft version, please help to modify, Thanks!
4-
******************************************************/
1+
/**
2+
* Find and retrieve the encryption key automatically.
3+
* @param {string} str - The input encrypted string.
4+
* @returns {number} - The encryption key found, or 0 if not found.
5+
*/
56
function keyFinder(str) {
67
// str is used to get the input of encrypted string
78
const wordBank = [
@@ -30,8 +31,6 @@ function keyFinder(str) {
3031
' be ',
3132
'Be '
3233
]
33-
// let wordbankelementCounter = 0;
34-
// let key = 0; // return zero means the key can not be found
3534
const inStr = str.toString() // convert the input to String
3635
let outStr = '' // store the output value
3736
let outStrElement = '' // temporary store the word inside the outStr, it is used for comparison
@@ -59,89 +58,95 @@ function keyFinder(str) {
5958
return 0 // return 0 if found nothing
6059
}
6160

62-
/* this sub-function is used to assist the keyFinder to find the key */
61+
/**
62+
* This sub-function is used to assist the keyFinder in finding the key.
63+
* @param {string} inStr - The input string.
64+
* @param {number} numShifted - The number of characters to shift in the Caesar cipher.
65+
* @returns {string} - The decrypted string.
66+
*/
6367
function caesarCipherEncodeAndDecodeEngine(inStr, numShifted) {
6468
const shiftNum = numShifted
6569
let charCode = 0
66-
let outStr = ''
6770
let shiftedCharCode = 0
6871
let result = 0
6972

70-
for (let i = 0; i < inStr.length; i++) {
71-
charCode = inStr[i].charCodeAt()
72-
shiftedCharCode = charCode + shiftNum
73-
result = charCode
73+
return inStr
74+
.split('')
75+
.map((char) => {
76+
charCode = char.charCodeAt()
77+
shiftedCharCode = charCode + shiftNum
78+
result = charCode
7479

75-
if (charCode >= 48 && charCode <= 57) {
76-
if (shiftedCharCode < 48) {
77-
let diff = Math.abs(48 - 1 - shiftedCharCode) % 10
80+
if (charCode >= 48 && charCode <= 57) {
81+
if (shiftedCharCode < 48) {
82+
let diff = Math.abs(48 - 1 - shiftedCharCode) % 10
7883

79-
while (diff >= 10) {
80-
diff = diff % 10
81-
}
82-
document.getElementById('diffID').innerHTML = diff
84+
while (diff >= 10) {
85+
diff = diff % 10
86+
}
87+
document.getElementById('diffID').innerHTML = diff
8388

84-
shiftedCharCode = 57 - diff
89+
shiftedCharCode = 57 - diff
8590

86-
result = shiftedCharCode
87-
} else if (shiftedCharCode >= 48 && shiftedCharCode <= 57) {
88-
result = shiftedCharCode
89-
} else if (shiftedCharCode > 57) {
90-
let diff = Math.abs(57 + 1 - shiftedCharCode) % 10
91+
result = shiftedCharCode
92+
} else if (shiftedCharCode >= 48 && shiftedCharCode <= 57) {
93+
result = shiftedCharCode
94+
} else if (shiftedCharCode > 57) {
95+
let diff = Math.abs(57 + 1 - shiftedCharCode) % 10
9196

92-
while (diff >= 10) {
93-
diff = diff % 10
94-
}
95-
document.getElementById('diffID').innerHTML = diff
97+
while (diff >= 10) {
98+
diff = diff % 10
99+
}
100+
document.getElementById('diffID').innerHTML = diff
96101

97-
shiftedCharCode = 48 + diff
102+
shiftedCharCode = 48 + diff
98103

99-
result = shiftedCharCode
100-
}
101-
} else if (charCode >= 65 && charCode <= 90) {
102-
if (shiftedCharCode <= 64) {
103-
let diff = Math.abs(65 - 1 - shiftedCharCode) % 26
104-
105-
while (diff % 26 >= 26) {
106-
diff = diff % 26
104+
result = shiftedCharCode
107105
}
108-
shiftedCharCode = 90 - diff
109-
result = shiftedCharCode
110-
} else if (shiftedCharCode >= 65 && shiftedCharCode <= 90) {
111-
result = shiftedCharCode
112-
} else if (shiftedCharCode > 90) {
113-
let diff = Math.abs(shiftedCharCode - 1 - 90) % 26
114-
115-
while (diff % 26 >= 26) {
116-
diff = diff % 26
106+
} else if (charCode >= 65 && charCode <= 90) {
107+
if (shiftedCharCode <= 64) {
108+
let diff = Math.abs(65 - 1 - shiftedCharCode) % 26
109+
110+
while (diff % 26 >= 26) {
111+
diff = diff % 26
112+
}
113+
shiftedCharCode = 90 - diff
114+
result = shiftedCharCode
115+
} else if (shiftedCharCode >= 65 && shiftedCharCode <= 90) {
116+
result = shiftedCharCode
117+
} else if (shiftedCharCode > 90) {
118+
let diff = Math.abs(shiftedCharCode - 1 - 90) % 26
119+
120+
while (diff % 26 >= 26) {
121+
diff = diff % 26
122+
}
123+
shiftedCharCode = 65 + diff
124+
result = shiftedCharCode
117125
}
118-
shiftedCharCode = 65 + diff
119-
result = shiftedCharCode
120-
}
121-
} else if (charCode >= 97 && charCode <= 122) {
122-
if (shiftedCharCode <= 96) {
123-
let diff = Math.abs(97 - 1 - shiftedCharCode) % 26
124-
125-
while (diff % 26 >= 26) {
126-
diff = diff % 26
127-
}
128-
shiftedCharCode = 122 - diff
129-
result = shiftedCharCode
130-
} else if (shiftedCharCode >= 97 && shiftedCharCode <= 122) {
131-
result = shiftedCharCode
132-
} else if (shiftedCharCode > 122) {
133-
let diff = Math.abs(shiftedCharCode - 1 - 122) % 26
134-
135-
while (diff % 26 >= 26) {
136-
diff = diff % 26
126+
} else if (charCode >= 97 && charCode <= 122) {
127+
if (shiftedCharCode <= 96) {
128+
let diff = Math.abs(97 - 1 - shiftedCharCode) % 26
129+
130+
while (diff % 26 >= 26) {
131+
diff = diff % 26
132+
}
133+
shiftedCharCode = 122 - diff
134+
result = shiftedCharCode
135+
} else if (shiftedCharCode >= 97 && shiftedCharCode <= 122) {
136+
result = shiftedCharCode
137+
} else if (shiftedCharCode > 122) {
138+
let diff = Math.abs(shiftedCharCode - 1 - 122) % 26
139+
140+
while (diff % 26 >= 26) {
141+
diff = diff % 26
142+
}
143+
shiftedCharCode = 97 + diff
144+
result = shiftedCharCode
137145
}
138-
shiftedCharCode = 97 + diff
139-
result = shiftedCharCode
140146
}
141-
}
142-
outStr = outStr + String.fromCharCode(parseInt(result))
143-
}
144-
return outStr
147+
return String.fromCharCode(parseInt(result))
148+
})
149+
.join('')
145150
}
146151

147152
export { keyFinder }

Conversions/RgbHslConversion.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Given a color in RGB format, convert it to HSL format.
3+
*
4+
* For more info: https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
5+
*
6+
* @param {number[]} colorRgb - One dimensional array of integers (RGB color format).
7+
* @returns {number[]} - One dimensional array of integers (HSL color format).
8+
*
9+
* @example
10+
* const colorRgb = [24, 98, 118]
11+
*
12+
* const result = rgbToHsl(colorRgb)
13+
*
14+
* // The function returns the corresponding color in HSL format:
15+
* // result = [193, 66, 28]
16+
*/
17+
18+
const checkRgbFormat = (colorRgb) => colorRgb.every((c) => c >= 0 && c <= 255)
19+
20+
const rgbToHsl = (colorRgb) => {
21+
if (!checkRgbFormat(colorRgb)) {
22+
throw new Error('Input is not a valid RGB color.')
23+
}
24+
25+
let colorHsl = colorRgb
26+
27+
let red = Math.round(colorRgb[0])
28+
let green = Math.round(colorRgb[1])
29+
let blue = Math.round(colorRgb[2])
30+
31+
const limit = 255
32+
33+
colorHsl[0] = red / limit
34+
colorHsl[1] = green / limit
35+
colorHsl[2] = blue / limit
36+
37+
let minValue = Math.min(...colorHsl)
38+
let maxValue = Math.max(...colorHsl)
39+
40+
let channel = 0
41+
42+
if (maxValue === colorHsl[1]) {
43+
channel = 1
44+
} else if (maxValue === colorHsl[2]) {
45+
channel = 2
46+
}
47+
48+
let luminance = (minValue + maxValue) / 2
49+
50+
let saturation = 0
51+
52+
if (minValue !== maxValue) {
53+
if (luminance <= 0.5) {
54+
saturation = (maxValue - minValue) / (maxValue + minValue)
55+
} else {
56+
saturation = (maxValue - minValue) / (2 - maxValue - minValue)
57+
}
58+
}
59+
60+
let hue = 0
61+
62+
if (saturation !== 0) {
63+
if (channel === 0) {
64+
hue = (colorHsl[1] - colorHsl[2]) / (maxValue - minValue)
65+
} else if (channel === 1) {
66+
hue = 2 + (colorHsl[2] - colorHsl[0]) / (maxValue - minValue)
67+
} else {
68+
hue = 4 + (colorHsl[0] - colorHsl[1]) / (maxValue - minValue)
69+
}
70+
}
71+
72+
hue *= 60
73+
74+
if (hue < 0) {
75+
hue += 360
76+
}
77+
78+
colorHsl[0] = Math.round(hue)
79+
colorHsl[1] = Math.round(saturation * 100)
80+
colorHsl[2] = Math.round(luminance * 100)
81+
82+
return colorHsl
83+
}
84+
85+
export { rgbToHsl }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { rgbToHsl } from '../RgbHslConversion'
2+
describe('RgbHslConversion', () => {
3+
test.each([
4+
[
5+
[215, 19, 180],
6+
[311, 84, 46]
7+
],
8+
[
9+
[21, 190, 18],
10+
[119, 83, 41]
11+
],
12+
[
13+
[80, 100, 160],
14+
[225, 33, 47]
15+
],
16+
[
17+
[80, 1, 16],
18+
[349, 98, 16]
19+
],
20+
[
21+
[8, 20, 0],
22+
[96, 100, 4]
23+
],
24+
[
25+
[0, 0, 0],
26+
[0, 0, 0]
27+
],
28+
[
29+
[255, 255, 255],
30+
[0, 0, 100]
31+
]
32+
])('Should return the color in HSL format.', (colorRgb, expected) => {
33+
expect(rgbToHsl(colorRgb)).toEqual(expected)
34+
})
35+
36+
test.each([
37+
[[256, 180, 9], 'Input is not a valid RGB color.'],
38+
[[-90, 46, 8], 'Input is not a valid RGB color.'],
39+
[[1, 39, 900], 'Input is not a valid RGB color.']
40+
])('Should return the error message.', (colorRgb, expected) => {
41+
expect(() => rgbToHsl(colorRgb)).toThrowError(expected)
42+
})
43+
})

DIRECTORY.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [SumOfSubset](Backtracking/SumOfSubset.js)
1010
* **Bit-Manipulation**
1111
* [BinaryCountSetBits](Bit-Manipulation/BinaryCountSetBits.js)
12+
* [GrayCodes](Bit-Manipulation/GrayCodes.js)
1213
* [IsPowerofFour](Bit-Manipulation/IsPowerofFour.js)
1314
* [IsPowerOfTwo](Bit-Manipulation/IsPowerOfTwo.js)
1415
* [LogTwo](Bit-Manipulation/LogTwo.js)
@@ -55,6 +56,7 @@
5556
* [OctToDecimal](Conversions/OctToDecimal.js)
5657
* [OuncesToKilograms](Conversions/OuncesToKilograms.js)
5758
* [RailwayTimeConversion](Conversions/RailwayTimeConversion.js)
59+
* [RgbHslConversion](Conversions/RgbHslConversion.js)
5860
* [RgbHsvConversion](Conversions/RgbHsvConversion.js)
5961
* [RGBToHex](Conversions/RGBToHex.js)
6062
* [RomanToDecimal](Conversions/RomanToDecimal.js)
@@ -72,15 +74,15 @@
7274
* [Graph2](Data-Structures/Graph/Graph2.js)
7375
* [Graph3](Data-Structures/Graph/Graph3.js)
7476
* **Heap**
77+
* [BinaryHeap](Data-Structures/Heap/BinaryHeap.js)
7578
* [KeyPriorityQueue](Data-Structures/Heap/KeyPriorityQueue.js)
76-
* [MaxHeap](Data-Structures/Heap/MaxHeap.js)
77-
* [MinHeap](Data-Structures/Heap/MinHeap.js)
7879
* [MinPriorityQueue](Data-Structures/Heap/MinPriorityQueue.js)
7980
* **Linked-List**
8081
* [AddTwoNumbers](Data-Structures/Linked-List/AddTwoNumbers.js)
8182
* [CycleDetection](Data-Structures/Linked-List/CycleDetection.js)
8283
* [CycleDetectionII](Data-Structures/Linked-List/CycleDetectionII.js)
8384
* [DoublyLinkedList](Data-Structures/Linked-List/DoublyLinkedList.js)
85+
* [MergeTwoSortedLinkedLists](Data-Structures/Linked-List/MergeTwoSortedLinkedLists.js)
8486
* [ReverseSinglyLinkedList](Data-Structures/Linked-List/ReverseSinglyLinkedList.js)
8587
* [SinglyCircularLinkedList](Data-Structures/Linked-List/SinglyCircularLinkedList.js)
8688
* [SinglyLinkedList](Data-Structures/Linked-List/SinglyLinkedList.js)
@@ -89,6 +91,7 @@
8991
* [Queue](Data-Structures/Queue/Queue.js)
9092
* [QueueUsing2Stacks](Data-Structures/Queue/QueueUsing2Stacks.js)
9193
* **Stack**
94+
* [EvaluateExpression](Data-Structures/Stack/EvaluateExpression.js)
9295
* [Stack](Data-Structures/Stack/Stack.js)
9396
* [StackES6](Data-Structures/Stack/StackES6.js)
9497
* **Tree**
@@ -180,6 +183,8 @@
180183
* [DecimalExpansion](Maths/DecimalExpansion.js)
181184
* [DecimalIsolate](Maths/DecimalIsolate.js)
182185
* [DegreeToRadian](Maths/DegreeToRadian.js)
186+
* [Determinant](Maths/Determinant.js)
187+
* [EuclideanDistance](Maths/EuclideanDistance.js)
183188
* [EulerMethod](Maths/EulerMethod.js)
184189
* [EulersTotient](Maths/EulersTotient.js)
185190
* [EulersTotientFunction](Maths/EulersTotientFunction.js)
@@ -239,6 +244,7 @@
239244
* [RadianToDegree](Maths/RadianToDegree.js)
240245
* [ReverseNumber](Maths/ReverseNumber.js)
241246
* [ReversePolishNotation](Maths/ReversePolishNotation.js)
247+
* [RowEchelon](Maths/RowEchelon.js)
242248
* [ShorsAlgorithm](Maths/ShorsAlgorithm.js)
243249
* [SieveOfEratosthenes](Maths/SieveOfEratosthenes.js)
244250
* [SieveOfEratosthenesIntArray](Maths/SieveOfEratosthenesIntArray.js)
@@ -250,6 +256,7 @@
250256
* [SumOfDigits](Maths/SumOfDigits.js)
251257
* [SumOfGeometricProgression](Maths/SumOfGeometricProgression.js)
252258
* [TwinPrime](Maths/TwinPrime.js)
259+
* [TwoSum](Maths/TwoSum.js)
253260
* [Volume](Maths/Volume.js)
254261
* [WhileLoopFactorial](Maths/WhileLoopFactorial.js)
255262
* [ZellersCongruenceAlgorithm](Maths/ZellersCongruenceAlgorithm.js)

0 commit comments

Comments
 (0)