Skip to content

Fixing non compliant files (Ciphers, Conversions and Data Structures) #140

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
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
132 changes: 79 additions & 53 deletions Ciphers/keyFinder.js
Original file line number Diff line number Diff line change
@@ -1,122 +1,148 @@
/******************************************************
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
Note: This is a draft version, please help to modify, Thanks!
******************************************************/
function keyFinder (str) { // str is used to get the input of encrypted string
const wordbank = ['I ', 'You ', 'We ', 'They ', 'He ', 'She ', 'It ', ' the ', 'The ', ' of ', ' is ', 'Is ', ' am ', 'Am ', ' are ', 'Are ', ' have ', 'Have ', ' has ', 'Has ', ' may ', 'May ', ' be ', 'Be ']
const wordBank = [
'I ',
'You ',
'We ',
'They ',
'He ',
'She ',
'It ',
' the ',
'The ',
' of ',
' is ',
'Is ',
' am ',
'Am ',
' are ',
'Are ',
' have ',
'Have ',
' has ',
'Has ',
' may ',
'May ',
' 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
for (let k = 0; k < 26; k++) { // try the number of key shifted, the sum of character from a-z or A-Z is 26
outStr = caesarCipherEncodeAndDecodeEngine(inStr, k) // use the encrytpion engine to decrypt the input string
for (let k = 0; k < 26; k++) { // try the number of key shifted, the sum of character from a-z or A-Z is 26
outStr = caesarCipherEncodeAndDecodeEngine(inStr, k) // use the encryption engine to decrypt the input string

// loop through the whole input string
for (let s = 0; s < outStr.length; s++) {
for (let i = 0; i < wordbank.length; i++) {
for (let i = 0; i < wordBank.length; i++) {
// initialize the outStrElement which is a temp output string for comparison,
// use a loop to find the next digit of wordbank element and compare with outStr's digit
for (let w = 0; w < wordbank[i].length; w++) {
// use a loop to find the next digit of wordBank element and compare with outStr's digit
for (let w = 0; w < wordBank[i].length; w++) {
outStrElement += outStr[s + w]
}

// console.log( k + outStrElement + wordbank[i] );//debug
// console.log( k + outStrElement + wordBank[i] );//debug

// this part need to be optimize with the calculation of the number of occurance of word's probabilities
// this part need to be optimize with the calculation of the number of occurrence of word's probabilities
// linked list will be used in the next stage of development to calculate the number of occurace of the key
if (wordbank[i] == outStrElement) {
return k // return the key number if founded
if (wordBank[i] === outStrElement) {
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
}

/* this sub-function is used to assist the keyfinder to find the key */
/* this sub-function is used to assist the keyFinder to find the key */
function caesarCipherEncodeAndDecodeEngine (inStr, numShifted) {
const shiftNum = numShifted
let charCode = 0
let outStr = ''
let shftedcharCode = 0
let shiftedCharCode = 0
let result = 0

for (let i = 0; i < inStr.length; i++) {
charCode = inStr[i].charCodeAt()
shftedcharCode = charCode + shiftNum
shiftedCharCode = charCode + shiftNum
result = charCode

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

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

shftedcharCode = 57 - diff
shiftedCharCode = 57 - diff

result = shftedcharCode
} else if (shftedcharCode >= 48 && shftedcharCode <= 57) {
result = shftedcharCode
} else if (shftedcharCode > 57) {
let diff = Math.abs(57 + 1 - shftedcharCode) % 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
diff = diff % 10
}
document.getElementById('diffID').innerHTML = diff

shftedcharCode = 48 + diff
shiftedCharCode = 48 + diff

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

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

while ((diff % 26) >= 26) {
diff = diff % 26
diff = diff % 26
}
shftedcharCode = 122 - diff
result = shftedcharCode
} else if (shftedcharCode >= 97 && shftedcharCode <= 122) {
result = shftedcharCode
} else if (shftedcharCode > 122) {
let diff = Math.abs(shftedcharCode - 1 - 122) % 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
diff = diff % 26
}
shftedcharCode = 97 + diff
result = shftedcharCode
shiftedCharCode = 97 + diff
result = shiftedCharCode
}
}
outStr = outStr + String.fromCharCode(parseInt(result))
}
return outStr
}

console.log('Testing: ' + keyFinder('test')) // returns 0
6 changes: 3 additions & 3 deletions Ciphers/vigenereCipher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Check if the Character is letter or not
* @param {String} character - character to check
* @param {String} str - character to check
* @return {object} An array with the character or null if isn't a letter
*/
function isLetter (str) {
Expand All @@ -13,10 +13,10 @@ function isLetter (str) {
* @return {Boolean} result of the checking
*/
function isUpperCase (character) {
if (character == character.toUpperCase()) {
if (character === character.toUpperCase()) {
return true
}
if (character == character.toLowerCase()) {
if (character === character.toLowerCase()) {
return false
}
}
Expand Down
6 changes: 3 additions & 3 deletions Conversions/DecimalToHex.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ function intToHex (num) {
}

function decimalToHex (num) {
const hex_out = []
const hexOut = []
while (num > 15) {
hex_out.push(intToHex(num % 16))
hexOut.push(intToHex(num % 16))
num = Math.floor(num / 16)
}
return intToHex(num) + hex_out.join('')
return intToHex(num) + hexOut.join('')
}

// test cases
Expand Down
9 changes: 4 additions & 5 deletions Data Structures/Heap/MinPriorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// Functions: insert, delete, peek, isEmpty, print, heapSort, sink

class MinPriorityQueue {
// calss the constructor and initializes the capacity
// calls the constructor and initializes the capacity
constructor (c) {
this.heap = []
this.capacity = c
Expand Down Expand Up @@ -43,13 +43,12 @@ class MinPriorityQueue {

// returns boolean value whether the heap is empty or not
isEmpty () {
if (this.size == 0) return true
return false
return this.size === 0
}

// returns boolean value whether the heap is full or not
isFull () {
if (this.size == this.capacity) return true
if (this.size === this.capacity) return true
return false
}

Expand Down Expand Up @@ -111,7 +110,7 @@ class MinPriorityQueue {
}

// testing
q = new MinPriorityQueue(8)
const q = new MinPriorityQueue(8)

q.insert(5)
q.insert(2)
Expand Down
Loading