From 2e0bd65b0c3dc1f0526cecd4ca068677034b0066 Mon Sep 17 00:00:00 2001 From: Juliano Nunes Date: Sun, 3 May 2020 21:26:52 -0300 Subject: [PATCH] Fixing non compliant files (Ciphers, Conversions and Data Structures) --- Ciphers/keyFinder.js | 132 +++--- Ciphers/vigenereCipher.js | 6 +- Conversions/DecimalToHex.js | 6 +- Data Structures/Heap/MinPriorityQueue.js | 9 +- .../Linked List/DoublyLinkedList.js | 376 +++++++++--------- Data Structures/Queue/Queue.js | 2 +- Data Structures/Stack/Stack.js | 2 +- Data Structures/Tree/Binary Search Tree.js | 2 +- 8 files changed, 282 insertions(+), 253 deletions(-) diff --git a/Ciphers/keyFinder.js b/Ciphers/keyFinder.js index 745810551a..8a66facc65 100644 --- a/Ciphers/keyFinder.js +++ b/Ciphers/keyFinder.js @@ -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 diff --git a/Ciphers/vigenereCipher.js b/Ciphers/vigenereCipher.js index e54b58cfcb..68ef3e85a0 100644 --- a/Ciphers/vigenereCipher.js +++ b/Ciphers/vigenereCipher.js @@ -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) { @@ -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 } } diff --git a/Conversions/DecimalToHex.js b/Conversions/DecimalToHex.js index 3b2167d8ed..0fa1b4c12c 100644 --- a/Conversions/DecimalToHex.js +++ b/Conversions/DecimalToHex.js @@ -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 diff --git a/Data Structures/Heap/MinPriorityQueue.js b/Data Structures/Heap/MinPriorityQueue.js index 02aff7699a..0b52ca65dc 100644 --- a/Data Structures/Heap/MinPriorityQueue.js +++ b/Data Structures/Heap/MinPriorityQueue.js @@ -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 @@ -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 } @@ -111,7 +110,7 @@ class MinPriorityQueue { } // testing -q = new MinPriorityQueue(8) +const q = new MinPriorityQueue(8) q.insert(5) q.insert(2) diff --git a/Data Structures/Linked List/DoublyLinkedList.js b/Data Structures/Linked List/DoublyLinkedList.js index e00401cb16..fdd8ea7418 100644 --- a/Data Structures/Linked List/DoublyLinkedList.js +++ b/Data Structures/Linked List/DoublyLinkedList.js @@ -1,198 +1,202 @@ -//Hamza chabchoub contribution for a university project -function doubleLinkedList() { - let Node = function(element) { - this.element = element; - this.next = null; - this.prev = null; - } - - let length = 0; - let head = null; - let tail = null; - - //Add new element - this.append = function(element) { - let node = new Node(element); - - if(!head){ - head = node; - tail = node; - }else{ - node.prev = tail; - tail.next = node; - tail = node; - } - - length++; +// Hamza chabchoub contribution for a university project +function DoubleLinkedList () { + const Node = function (element) { + this.element = element + this.next = 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 } - - - //Add element - this.insert = function(position, element) { - - //Check of out-of-bound values - if(position >= 0 && position <= length){ - 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++ + } + + // Add element + this.insert = function (position, 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 (position === 0) { + if (!head) { + head = node + tail = node + } else { + node.next = current + current.prev = node + head = node } - - length++; - return true; - }else{ - return false; - } - } - - //Remove element at any position - this.removeAt = function(position){ - //look for out-of-bounds value - 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; + } else if (position === length) { + current = tail + current.next = node + node.prev = current + tail = node + } else { + while (index++ < position) { + previous = current + current = current.next } - - length--; - return current.element; - }else{ - return null; + + node.next = current + previous.next = node + + // New + current.prev = node + node.prev = previous } + + length++ + return true + } else { + return false } - - //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; + } + + // 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 } - - 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; + } 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 } - - return string; - }; - - //Convert list to array - this.toArray = function(){ - let arr = [], - current = head; - - while(current){ - arr.push(current.element); - current = current.next; + + 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 } - - return arr; - }; - - //Check if list is empty - this.isEmpty = function(){ - return length === 0; - }; - - //Get the size of the list - this.size = function(){ - return length; + + index++ + current = current.next } - - //Get the head - this.getHead = function() { - return head; + + // 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 } - - //Get the tail - this.getTail = function() { - return tail; + + 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 diff --git a/Data Structures/Queue/Queue.js b/Data Structures/Queue/Queue.js index d02d651e4a..aaab824cd6 100644 --- a/Data Structures/Queue/Queue.js +++ b/Data Structures/Queue/Queue.js @@ -23,7 +23,7 @@ var Queue = (function () { // Removes the value at the front of the queue Queue.prototype.dequeue = function () { if (this.queue.length === 0) { - throw 'Queue is Empty' + throw new Error('Queue is Empty') } var result = this.queue[0] diff --git a/Data Structures/Stack/Stack.js b/Data Structures/Stack/Stack.js index cc79442ce5..8be28006a7 100644 --- a/Data Structures/Stack/Stack.js +++ b/Data Structures/Stack/Stack.js @@ -13,7 +13,7 @@ var Stack = (function () { // The top of the Stack this.top = 0 // The array representation of the stack - this.stack = new Array() + this.stack = [] } // Adds a value onto the end of the stack diff --git a/Data Structures/Tree/Binary Search Tree.js b/Data Structures/Tree/Binary Search Tree.js index 35edb54508..943332d54c 100644 --- a/Data Structures/Tree/Binary Search Tree.js +++ b/Data Structures/Tree/Binary Search Tree.js @@ -21,7 +21,7 @@ var Node = (function () { // Search the tree for a value Node.prototype.search = function (val) { - if (this.value == val) { + if (this.value === val) { return this } else if (val < this.value && this.left != null) { return this.left.search(val)