Skip to content

[pull] master from TheAlgorithms:master #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2023
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
149 changes: 77 additions & 72 deletions Ciphers/KeyFinder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/******************************************************
Find and retrieve the encryption key automatically
Note: This is a draft version, please help to modify, Thanks!
******************************************************/
/**
* Find and retrieve the encryption key automatically.
* @param {string} str - The input encrypted string.
* @returns {number} - The encryption key found, or 0 if not found.
*/
function keyFinder(str) {
// str is used to get the input of encrypted string
const wordBank = [
Expand Down Expand Up @@ -30,8 +31,6 @@ function keyFinder(str) {
' be ',
'Be '
]
// let wordbankelementCounter = 0;
// let key = 0; // return zero means the key can not be found
const inStr = str.toString() // convert the input to String
let outStr = '' // store the output value
let outStrElement = '' // temporary store the word inside the outStr, it is used for comparison
Expand Down Expand Up @@ -59,89 +58,95 @@ function keyFinder(str) {
return 0 // return 0 if found nothing
}

/* this sub-function is used to assist the keyFinder to find the key */
/**
* This sub-function is used to assist the keyFinder in finding the key.
* @param {string} inStr - The input string.
* @param {number} numShifted - The number of characters to shift in the Caesar cipher.
* @returns {string} - The decrypted string.
*/
function caesarCipherEncodeAndDecodeEngine(inStr, numShifted) {
const shiftNum = numShifted
let charCode = 0
let outStr = ''
let shiftedCharCode = 0
let result = 0

for (let i = 0; i < inStr.length; i++) {
charCode = inStr[i].charCodeAt()
shiftedCharCode = charCode + shiftNum
result = charCode
return inStr
.split('')
.map((char) => {
charCode = char.charCodeAt()
shiftedCharCode = charCode + shiftNum
result = charCode

if (charCode >= 48 && charCode <= 57) {
if (shiftedCharCode < 48) {
let diff = Math.abs(48 - 1 - shiftedCharCode) % 10
if (charCode >= 48 && charCode <= 57) {
if (shiftedCharCode < 48) {
let diff = Math.abs(48 - 1 - shiftedCharCode) % 10

while (diff >= 10) {
diff = diff % 10
}
document.getElementById('diffID').innerHTML = diff
while (diff >= 10) {
diff = diff % 10
}
document.getElementById('diffID').innerHTML = diff

shiftedCharCode = 57 - diff
shiftedCharCode = 57 - diff

result = shiftedCharCode
} else if (shiftedCharCode >= 48 && shiftedCharCode <= 57) {
result = shiftedCharCode
} else if (shiftedCharCode > 57) {
let diff = Math.abs(57 + 1 - shiftedCharCode) % 10
result = shiftedCharCode
} else if (shiftedCharCode >= 48 && shiftedCharCode <= 57) {
result = shiftedCharCode
} else if (shiftedCharCode > 57) {
let diff = Math.abs(57 + 1 - shiftedCharCode) % 10

while (diff >= 10) {
diff = diff % 10
}
document.getElementById('diffID').innerHTML = diff
while (diff >= 10) {
diff = diff % 10
}
document.getElementById('diffID').innerHTML = diff

shiftedCharCode = 48 + diff
shiftedCharCode = 48 + diff

result = shiftedCharCode
}
} else if (charCode >= 65 && charCode <= 90) {
if (shiftedCharCode <= 64) {
let diff = Math.abs(65 - 1 - shiftedCharCode) % 26

while (diff % 26 >= 26) {
diff = diff % 26
result = shiftedCharCode
}
shiftedCharCode = 90 - diff
result = shiftedCharCode
} else if (shiftedCharCode >= 65 && shiftedCharCode <= 90) {
result = shiftedCharCode
} else if (shiftedCharCode > 90) {
let diff = Math.abs(shiftedCharCode - 1 - 90) % 26

while (diff % 26 >= 26) {
diff = diff % 26
} else if (charCode >= 65 && charCode <= 90) {
if (shiftedCharCode <= 64) {
let diff = Math.abs(65 - 1 - shiftedCharCode) % 26

while (diff % 26 >= 26) {
diff = diff % 26
}
shiftedCharCode = 90 - diff
result = shiftedCharCode
} else if (shiftedCharCode >= 65 && shiftedCharCode <= 90) {
result = shiftedCharCode
} else if (shiftedCharCode > 90) {
let diff = Math.abs(shiftedCharCode - 1 - 90) % 26

while (diff % 26 >= 26) {
diff = diff % 26
}
shiftedCharCode = 65 + diff
result = shiftedCharCode
}
shiftedCharCode = 65 + diff
result = shiftedCharCode
}
} else if (charCode >= 97 && charCode <= 122) {
if (shiftedCharCode <= 96) {
let diff = Math.abs(97 - 1 - shiftedCharCode) % 26

while (diff % 26 >= 26) {
diff = diff % 26
}
shiftedCharCode = 122 - diff
result = shiftedCharCode
} else if (shiftedCharCode >= 97 && shiftedCharCode <= 122) {
result = shiftedCharCode
} else if (shiftedCharCode > 122) {
let diff = Math.abs(shiftedCharCode - 1 - 122) % 26

while (diff % 26 >= 26) {
diff = diff % 26
} else if (charCode >= 97 && charCode <= 122) {
if (shiftedCharCode <= 96) {
let diff = Math.abs(97 - 1 - shiftedCharCode) % 26

while (diff % 26 >= 26) {
diff = diff % 26
}
shiftedCharCode = 122 - diff
result = shiftedCharCode
} else if (shiftedCharCode >= 97 && shiftedCharCode <= 122) {
result = shiftedCharCode
} else if (shiftedCharCode > 122) {
let diff = Math.abs(shiftedCharCode - 1 - 122) % 26

while (diff % 26 >= 26) {
diff = diff % 26
}
shiftedCharCode = 97 + diff
result = shiftedCharCode
}
shiftedCharCode = 97 + diff
result = shiftedCharCode
}
}
outStr = outStr + String.fromCharCode(parseInt(result))
}
return outStr
return String.fromCharCode(parseInt(result))
})
.join('')
}

export { keyFinder }
Expand Down
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
* [DecimalExpansion](Maths/DecimalExpansion.js)
* [DecimalIsolate](Maths/DecimalIsolate.js)
* [DegreeToRadian](Maths/DegreeToRadian.js)
* [EuclideanDistance](Maths/EuclideanDistance.js)
* [EulerMethod](Maths/EulerMethod.js)
* [EulersTotient](Maths/EulersTotient.js)
* [EulersTotientFunction](Maths/EulersTotientFunction.js)
Expand Down Expand Up @@ -249,6 +250,7 @@
* [SumOfDigits](Maths/SumOfDigits.js)
* [SumOfGeometricProgression](Maths/SumOfGeometricProgression.js)
* [TwinPrime](Maths/TwinPrime.js)
* [TwoSum](Maths/TwoSum.js)
* [Volume](Maths/Volume.js)
* [WhileLoopFactorial](Maths/WhileLoopFactorial.js)
* [ZellersCongruenceAlgorithm](Maths/ZellersCongruenceAlgorithm.js)
Expand Down
151 changes: 151 additions & 0 deletions Data-Structures/Heap/BinaryHeap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* BinaryHeap class represents a binary heap data structure that can be configured as a Min Heap or Max Heap.
*
* Binary heaps are binary trees that are filled level by level and from left to right inside each level.
* They have the property that any parent node has a smaller (for Min Heap) or greater (for Max Heap) priority
* than its children, ensuring that the root of the tree always holds the extremal value.
*/
class BinaryHeap {
/**
* Creates a new BinaryHeap instance.
* @constructor
* @param {Function} comparatorFunction - The comparator function used to determine the order of elements (e.g., minHeapComparator or maxHeapComparator).
*/
constructor(comparatorFunction) {
/**
* The heap array that stores elements.
* @member {Array}
*/
this.heap = []

/**
* The comparator function used for ordering elements in the heap.
* @member {Function}
*/
this.comparator = comparatorFunction
}

/**
* Inserts a new value into the heap.
* @param {*} value - The value to be inserted into the heap.
*/
insert(value) {
this.heap.push(value)
this.#bubbleUp(this.heap.length - 1)
}

/**
* Returns the number of elements in the heap.
* @returns {number} - The number of elements in the heap.
*/
size() {
return this.heap.length
}

/**
* Checks if the heap is empty.
* @returns {boolean} - True if the heap is empty, false otherwise.
*/
empty() {
return this.size() === 0
}

/**
* Bubbles up a value from the specified index to maintain the heap property.
* @param {number} currIdx - The index of the value to be bubbled up.
* @private
*/
#bubbleUp(currIdx) {
let parentIdx = Math.floor((currIdx - 1) / 2)

while (
currIdx > 0 &&
this.comparator(this.heap[currIdx], this.heap[parentIdx])
) {
this.#swap(currIdx, parentIdx)
currIdx = parentIdx
parentIdx = Math.floor((currIdx - 1) / 2)
}
}

/**
* Sinks down a value from the specified index to maintain the heap property.
* @param {number} currIdx - The index of the value to be sunk down.
* @private
*/
#sinkDown(currIdx) {
let childOneIdx = currIdx * 2 + 1

while (childOneIdx < this.size()) {
const childTwoIdx = childOneIdx + 1 < this.size() ? childOneIdx + 1 : -1
const swapIdx =
childTwoIdx !== -1 &&
this.comparator(this.heap[childTwoIdx], this.heap[childOneIdx])
? childTwoIdx
: childOneIdx

if (this.comparator(this.heap[swapIdx], this.heap[currIdx])) {
this.#swap(currIdx, swapIdx)
currIdx = swapIdx
childOneIdx = currIdx * 2 + 1
} else {
return
}
}
}

/**
* Retrieves the top element of the heap without removing it.
* @returns {*} - The top element of the heap.
*/
peek() {
return this.heap[0]
}

/**
* Removes and returns the top element of the heap.
* @returns {*} - The top element of the heap.
*/
extractTop() {
const top = this.peek()
const last = this.heap.pop()

if (!this.empty()) {
this.heap[0] = last
this.#sinkDown(0)
}

return top
}

/**
* Swaps elements at two specified indices in the heap.
* @param {number} index1 - The index of the first element to be swapped.
* @param {number} index2 - The index of the second element to be swapped.
* @private
*/
#swap(index1, index2) {
;[this.heap[index1], this.heap[index2]] = [
this.heap[index2],
this.heap[index1]
]
}
}

/**
* Comparator function for creating a Min Heap.
* @param {*} a - The first element to compare.
* @param {*} b - The second element to compare.
* @returns {boolean} - True if 'a' should have higher priority than 'b' in the Min Heap, false otherwise.
*/
const minHeapComparator = (a, b) => a < b

/**
* Comparator function for creating a Max Heap.
* @param {*} a - The first element to compare.
* @param {*} b - The second element to compare.
* @returns {boolean} - True if 'a' should have higher priority than 'b' in the Max Heap, false otherwise.
*/
const maxHeapComparator = (a, b) => a > b

export { BinaryHeap, minHeapComparator, maxHeapComparator }
Loading