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

This commit is contained in:
Juliano Nunes
2020-05-03 21:26:52 -03:00
parent 14bab1b931
commit 2e0bd65b0c
8 changed files with 293 additions and 264 deletions

View File

@ -1,122 +1,148 @@
/****************************************************** /******************************************************
Find and retrieve the encryption key automatically Find and retrieve the encryption key automatically
Note: This is a draft version, please help to modify, Thanks! Note: This is a draft version, please help to modify, Thanks!
******************************************************/ ******************************************************/
function keyFinder (str) { // str is used to get the input of encrypted string 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 wordbankelementCounter = 0;
// let key = 0; // return zero means the key can not be found // let key = 0; // return zero means the key can not be found
const inStr = str.toString() // convert the input to String const inStr = str.toString() // convert the input to String
let outStr = '' // store the output value let outStr = '' // store the output value
let outStrElement = '' // temporary store the word inside the outStr, it is used for comparison 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 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 outStr = caesarCipherEncodeAndDecodeEngine(inStr, k) // use the encryption engine to decrypt the input string
// loop through the whole input string // loop through the whole input string
for (let s = 0; s < outStr.length; s++) { 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, // 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 // 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++) { for (let w = 0; w < wordBank[i].length; w++) {
outStrElement += outStr[s + 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 // linked list will be used in the next stage of development to calculate the number of occurace of the key
if (wordbank[i] == outStrElement) { if (wordBank[i] === outStrElement) {
return k // return the key number if founded return k // return the key number if founded
} }
outStrElement = '' // reset the temp word 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 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) { function caesarCipherEncodeAndDecodeEngine (inStr, numShifted) {
const shiftNum = numShifted const shiftNum = numShifted
let charCode = 0 let charCode = 0
let outStr = '' let outStr = ''
let shftedcharCode = 0 let shiftedCharCode = 0
let result = 0 let result = 0
for (let i = 0; i < inStr.length; i++) { for (let i = 0; i < inStr.length; i++) {
charCode = inStr[i].charCodeAt() charCode = inStr[i].charCodeAt()
shftedcharCode = charCode + shiftNum shiftedCharCode = charCode + shiftNum
result = charCode result = charCode
if ((charCode >= 48 && charCode <= 57)) { if ((charCode >= 48 && charCode <= 57)) {
if (shftedcharCode < 48) { if (shiftedCharCode < 48) {
let diff = Math.abs(48 - 1 - shftedcharCode) % 10 let diff = Math.abs(48 - 1 - shiftedCharCode) % 10
while (diff >= 10) { while (diff >= 10) {
diff = diff % 10 diff = diff % 10
} }
document.getElementById('diffID').innerHTML = diff document.getElementById('diffID').innerHTML = diff
shftedcharCode = 57 - diff shiftedCharCode = 57 - diff
result = shftedcharCode result = shiftedCharCode
} else if (shftedcharCode >= 48 && shftedcharCode <= 57) { } else if (shiftedCharCode >= 48 && shiftedCharCode <= 57) {
result = shftedcharCode result = shiftedCharCode
} else if (shftedcharCode > 57) { } else if (shiftedCharCode > 57) {
let diff = Math.abs(57 + 1 - shftedcharCode) % 10 let diff = Math.abs(57 + 1 - shiftedCharCode) % 10
while (diff >= 10) { while (diff >= 10) {
diff = diff % 10 diff = diff % 10
} }
document.getElementById('diffID').innerHTML = diff document.getElementById('diffID').innerHTML = diff
shftedcharCode = 48 + diff shiftedCharCode = 48 + diff
result = shftedcharCode result = shiftedCharCode
} }
} else if ((charCode >= 65 && charCode <= 90)) { } else if ((charCode >= 65 && charCode <= 90)) {
if (shftedcharCode <= 64) { if (shiftedCharCode <= 64) {
let diff = Math.abs(65 - 1 - shftedcharCode) % 26 let diff = Math.abs(65 - 1 - shiftedCharCode) % 26
while ((diff % 26) >= 26) { while ((diff % 26) >= 26) {
diff = diff % 26 diff = diff % 26
} }
shftedcharCode = 90 - diff shiftedCharCode = 90 - diff
result = shftedcharCode result = shiftedCharCode
} else if (shftedcharCode >= 65 && shftedcharCode <= 90) { } else if (shiftedCharCode >= 65 && shiftedCharCode <= 90) {
result = shftedcharCode result = shiftedCharCode
} else if (shftedcharCode > 90) { } else if (shiftedCharCode > 90) {
let diff = Math.abs(shftedcharCode - 1 - 90) % 26 let diff = Math.abs(shiftedCharCode - 1 - 90) % 26
while ((diff % 26) >= 26) { while ((diff % 26) >= 26) {
diff = diff % 26 diff = diff % 26
} }
shftedcharCode = 65 + diff shiftedCharCode = 65 + diff
result = shftedcharCode result = shiftedCharCode
} }
} else if ((charCode >= 97 && charCode <= 122)) { } else if ((charCode >= 97 && charCode <= 122)) {
if (shftedcharCode <= 96) { if (shiftedCharCode <= 96) {
let diff = Math.abs(97 - 1 - shftedcharCode) % 26 let diff = Math.abs(97 - 1 - shiftedCharCode) % 26
while ((diff % 26) >= 26) { while ((diff % 26) >= 26) {
diff = diff % 26 diff = diff % 26
} }
shftedcharCode = 122 - diff shiftedCharCode = 122 - diff
result = shftedcharCode result = shiftedCharCode
} else if (shftedcharCode >= 97 && shftedcharCode <= 122) { } else if (shiftedCharCode >= 97 && shiftedCharCode <= 122) {
result = shftedcharCode result = shiftedCharCode
} else if (shftedcharCode > 122) { } else if (shiftedCharCode > 122) {
let diff = Math.abs(shftedcharCode - 1 - 122) % 26 let diff = Math.abs(shiftedCharCode - 1 - 122) % 26
while ((diff % 26) >= 26) { while ((diff % 26) >= 26) {
diff = diff % 26 diff = diff % 26
} }
shftedcharCode = 97 + diff shiftedCharCode = 97 + diff
result = shftedcharCode result = shiftedCharCode
} }
} }
outStr = outStr + String.fromCharCode(parseInt(result)) outStr = outStr + String.fromCharCode(parseInt(result))
} }
return outStr return outStr
} }
console.log('Testing: ' + keyFinder('test')) // returns 0

View File

@ -1,6 +1,6 @@
/** /**
* Check if the Character is letter or not * 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 * @return {object} An array with the character or null if isn't a letter
*/ */
function isLetter (str) { function isLetter (str) {
@ -13,10 +13,10 @@ function isLetter (str) {
* @return {Boolean} result of the checking * @return {Boolean} result of the checking
*/ */
function isUpperCase (character) { function isUpperCase (character) {
if (character == character.toUpperCase()) { if (character === character.toUpperCase()) {
return true return true
} }
if (character == character.toLowerCase()) { if (character === character.toLowerCase()) {
return false return false
} }
} }

View File

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

View File

@ -12,7 +12,7 @@
// Functions: insert, delete, peek, isEmpty, print, heapSort, sink // Functions: insert, delete, peek, isEmpty, print, heapSort, sink
class MinPriorityQueue { class MinPriorityQueue {
// calss the constructor and initializes the capacity // calls the constructor and initializes the capacity
constructor (c) { constructor (c) {
this.heap = [] this.heap = []
this.capacity = c this.capacity = c
@ -43,13 +43,12 @@ class MinPriorityQueue {
// returns boolean value whether the heap is empty or not // returns boolean value whether the heap is empty or not
isEmpty () { isEmpty () {
if (this.size == 0) return true return this.size === 0
return false
} }
// returns boolean value whether the heap is full or not // returns boolean value whether the heap is full or not
isFull () { isFull () {
if (this.size == this.capacity) return true if (this.size === this.capacity) return true
return false return false
} }
@ -111,7 +110,7 @@ class MinPriorityQueue {
} }
// testing // testing
q = new MinPriorityQueue(8) const q = new MinPriorityQueue(8)
q.insert(5) q.insert(5)
q.insert(2) q.insert(2)

View File

@ -1,198 +1,202 @@
//Hamza chabchoub contribution for a university project // Hamza chabchoub contribution for a university project
function doubleLinkedList() { function DoubleLinkedList () {
let Node = function(element) { const Node = function (element) {
this.element = element; this.element = element
this.next = null; this.next = null
this.prev = null; this.prev = null
}
let length = 0
let head = null
let tail = null
// Add new element
this.append = function (element) {
const node = new Node(element)
if (!head) {
head = node
tail = node
} else {
node.prev = tail
tail.next = node
tail = node
} }
let length = 0; length++
let head = null; }
let tail = null;
//Add new element // Add element
this.append = function(element) { this.insert = function (position, element) {
let node = new Node(element); // Check of out-of-bound values
if (position >= 0 && position <= length) {
const node = new Node(element)
let current = head
let previous = 0
let index = 0
if(!head){ if (position === 0) {
head = node; if (!head) {
tail = node; head = node
}else{ tail = node
node.prev = tail; } else {
tail.next = node; node.next = current
tail = node; current.prev = node
} head = node
}
length++; } else if (position === length) {
} current = tail
current.next = node
node.prev = current
//Add element tail = node
this.insert = function(position, element) { } else {
while (index++ < position) {
//Check of out-of-bound values previous = current
if(position >= 0 && position <= length){ current = current.next
let node = new Node(element),
current = head,
previous,
index = 0;
if(position === 0){
if(!head){
head = node;
tail = node;
}else{
node.next = current;
current.prev = node;
head = node;
}
}else if(position === length){
current = tail;
current.next = node;
node.prev = current;
tail = node;
}else{
while(index++ < position){
previous = current;
current = current.next;
}
node.next = current;
previous.next = node;
//New
current.prev = node;
node.prev = previous;
} }
length++; node.next = current
return true; previous.next = node
}else{
return false;
}
}
//Remove element at any position // New
this.removeAt = function(position){ current.prev = node
//look for out-of-bounds value node.prev = previous
if(position > -1 && position < length){
let current = head, previous, index = 0;
//Removing first item
if(position === 0){
head = current.next;
//if there is only one item, update tail //NEW
if(length === 1){
tail = null;
}else{
head.prev = null;
}
}else if(position === length - 1){
current = tail;
tail = current.prev;
tail.next = null;
}else{
while(index++ < position){
previous = current;
current = current.next;
}
//link previous with current's next - skip it
previous.next = current.next;
current.next.prev = previous;
}
length--;
return current.element;
}else{
return null;
}
}
//Get the indexOf item
this.indexOf = function(elm){
let current = head,
index = -1;
//If element found then return its position
while(current){
if(elm === current.element){
return ++index;
}
index++;
current = current.next;
}
//Else return -1
return -1;
};
//Find the item in the list
this.isPresent = (elm) => {
return this.indexOf(elm) !== -1;
};
//Delete an item from the list
this.delete = (elm) => {
return this.removeAt(this.indexOf(elm));
};
//Delete first item from the list
this.deleteHead = function(){
this.removeAt(0);
}
//Delete last item from the list
this.deleteTail = function(){
this.removeAt(length-1);
}
//Print item of the string
this.toString = function(){
let current = head,
string = '';
while(current){
string += current.element + (current.next ? '\n' : '');
current = current.next;
} }
return string; length++
}; return true
} else {
//Convert list to array return false
this.toArray = function(){
let arr = [],
current = head;
while(current){
arr.push(current.element);
current = current.next;
}
return arr;
};
//Check if list is empty
this.isEmpty = function(){
return length === 0;
};
//Get the size of the list
this.size = function(){
return length;
}
//Get the head
this.getHead = function() {
return head;
}
//Get the tail
this.getTail = function() {
return tail;
} }
} }
// Remove element at any position
this.removeAt = function (position) {
// look for out-of-bounds value
if (position > -1 && position < length) {
let current = head
let previous = 0
let index = 0
// Removing first item
if (position === 0) {
head = current.next
// if there is only one item, update tail //NEW
if (length === 1) {
tail = null
} else {
head.prev = null
}
} else if (position === length - 1) {
current = tail
tail = current.prev
tail.next = null
} else {
while (index++ < position) {
previous = current
current = current.next
}
// link previous with current's next - skip it
previous.next = current.next
current.next.prev = previous
}
length--
return current.element
} else {
return null
}
}
// Get the indexOf item
this.indexOf = function (elm) {
let current = head
let index = -1
// If element found then return its position
while (current) {
if (elm === current.element) {
return ++index
}
index++
current = current.next
}
// Else return -1
return -1
}
// Find the item in the list
this.isPresent = (elm) => {
return this.indexOf(elm) !== -1
}
// Delete an item from the list
this.delete = (elm) => {
return this.removeAt(this.indexOf(elm))
}
// Delete first item from the list
this.deleteHead = function () {
this.removeAt(0)
}
// Delete last item from the list
this.deleteTail = function () {
this.removeAt(length - 1)
}
// Print item of the string
this.toString = function () {
let current = head
let string = ''
while (current) {
string += current.element + (current.next ? '\n' : '')
current = current.next
}
return string
}
// Convert list to array
this.toArray = function () {
const arr = []
let current = head
while (current) {
arr.push(current.element)
current = current.next
}
return arr
}
// Check if list is empty
this.isEmpty = function () {
return length === 0
}
// Get the size of the list
this.size = function () {
return length
}
// Get the head
this.getHead = function () {
return head
}
// Get the tail
this.getTail = function () {
return tail
}
}
const newDoubleLinkedList = new DoubleLinkedList()
newDoubleLinkedList.append(1)
newDoubleLinkedList.append(2)
console.log('Testing: ' + newDoubleLinkedList.size()) // returns 2

View File

@ -23,7 +23,7 @@ var Queue = (function () {
// Removes the value at the front of the queue // Removes the value at the front of the queue
Queue.prototype.dequeue = function () { Queue.prototype.dequeue = function () {
if (this.queue.length === 0) { if (this.queue.length === 0) {
throw 'Queue is Empty' throw new Error('Queue is Empty')
} }
var result = this.queue[0] var result = this.queue[0]

View File

@ -13,7 +13,7 @@ var Stack = (function () {
// The top of the Stack // The top of the Stack
this.top = 0 this.top = 0
// The array representation of the stack // The array representation of the stack
this.stack = new Array() this.stack = []
} }
// Adds a value onto the end of the stack // Adds a value onto the end of the stack

View File

@ -21,7 +21,7 @@ var Node = (function () {
// Search the tree for a value // Search the tree for a value
Node.prototype.search = function (val) { Node.prototype.search = function (val) {
if (this.value == val) { if (this.value === val) {
return this return this
} else if (val < this.value && this.left != null) { } else if (val < this.value && this.left != null) {
return this.left.search(val) return this.left.search(val)