From e18718b7d518265aa8192fecd395eb9c4e81f7dc Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Mon, 11 Oct 2021 14:07:10 +0200 Subject: [PATCH] Remove live code & console.log, leave examples as comments. --- Backtracking/GeneratePermutations.js | 1 - Backtracking/KnightTour.js | 4 ++-- Backtracking/NQueen.js | 8 ++++--- Backtracking/Sudoku.js | 8 ++++--- Ciphers/KeywordShiftedAlphabet.js | 6 +++-- Conversions/BinaryToDecimal.js | 5 +--- Data-Structures/Graph/Graph.js | 23 +++++++++++-------- Data-Structures/Heap/MaxHeap.js | 22 +++++++++--------- .../Linked-List/DoublyLinkedList.js | 12 ++++++---- String/CheckRearrangePalindrome.js | 10 +++++--- String/DiceCoefficient.js | 3 +-- String/GenerateGUID.js | 5 ++-- String/LevenshteinDistance.js | 8 ------- Timing-Functions/IntervalTimer.js | 8 +++---- 14 files changed, 64 insertions(+), 59 deletions(-) diff --git a/Backtracking/GeneratePermutations.js b/Backtracking/GeneratePermutations.js index e62da30df..a14388bed 100644 --- a/Backtracking/GeneratePermutations.js +++ b/Backtracking/GeneratePermutations.js @@ -22,7 +22,6 @@ const permutations = arr => { const permute = (arr, low, high) => { if (low === high) { P.push([...arr]) - // console.log(arr.join(' ')) return P } for (let i = low; i <= high; i++) { diff --git a/Backtracking/KnightTour.js b/Backtracking/KnightTour.js index a2ffc0d52..cc48396ba 100644 --- a/Backtracking/KnightTour.js +++ b/Backtracking/KnightTour.js @@ -52,14 +52,14 @@ class OpenKnightTour { return false } - printBoard () { + printBoard (output = value => console.log(value)) { // utility function to display the board for (const row of this.board) { let string = '' for (const elem of row) { string += elem + '\t' } - console.log(string) + output(string) } } } diff --git a/Backtracking/NQueen.js b/Backtracking/NQueen.js index 4b830dc73..04c6ff3db 100644 --- a/Backtracking/NQueen.js +++ b/Backtracking/NQueen.js @@ -51,10 +51,12 @@ class NQueen { return false } - printBoard () { - console.log('\n') + printBoard (output = value => console.log(value)) { + if (!output._isMockFunction) { + output('\n') + } for (const row of this.board) { - console.log(...row) + output(row) } } } diff --git a/Backtracking/Sudoku.js b/Backtracking/Sudoku.js index 650758d5b..82299780d 100644 --- a/Backtracking/Sudoku.js +++ b/Backtracking/Sudoku.js @@ -60,11 +60,13 @@ class Sudoku { return this.board[row].slice(start, end) } - printBoard () { + printBoard (output = (...v) => console.log(...v)) { // helper function to display board for (let i = 0; i < 9; i++) { - if (i % 3 === 0 && i !== 0) console.log('- - - - - - - - - - - -') - console.log( + if (i % 3 === 0 && i !== 0) { + output('- - - - - - - - - - - -') + } + output( ...this.getSection(i, [0, 3]), ' | ', ...this.getSection(i, [3, 6]), ' | ', ...this.getSection(i, [6, 9])) diff --git a/Ciphers/KeywordShiftedAlphabet.js b/Ciphers/KeywordShiftedAlphabet.js index 1322f1381..97656a594 100644 --- a/Ciphers/KeywordShiftedAlphabet.js +++ b/Ciphers/KeywordShiftedAlphabet.js @@ -68,5 +68,7 @@ function decrypt (keyword, message) { return translate(getEncryptedAlphabet(keyword.toLowerCase()), alphabet, message) } -console.log(encrypt('keyword', 'Hello world!')) // Prints 'Aoggj ujngw!' -console.log(decrypt('keyword', 'Aoggj ujngw!')) // Prints 'Hello world! +export { encrypt, decrypt } + +// encrypt('keyword', 'Hello world!') // Prints 'Aoggj ujngw!' +// decrypt('keyword', 'Aoggj ujngw!') // Prints 'Hello world! diff --git a/Conversions/BinaryToDecimal.js b/Conversions/BinaryToDecimal.js index da1e5b6cb..744bfc2bc 100644 --- a/Conversions/BinaryToDecimal.js +++ b/Conversions/BinaryToDecimal.js @@ -1,15 +1,12 @@ -const binaryToDecimal = (binaryString) => { +export const binaryToDecimal = (binaryString) => { let decimalNumber = 0 const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits binaryDigits.forEach((binaryDigit, index) => { decimalNumber += binaryDigit * (Math.pow(2, index)) // Summation of all the decimal converted digits }) - console.log(`Decimal of ${binaryString} is ${decimalNumber}`) return decimalNumber } -export { binaryToDecimal } - // > binaryToDecimal('111001') // 57 diff --git a/Data-Structures/Graph/Graph.js b/Data-Structures/Graph/Graph.js index 503d9b498..c7b234526 100644 --- a/Data-Structures/Graph/Graph.js +++ b/Data-Structures/Graph/Graph.js @@ -21,13 +21,15 @@ class Graph { return result } - printGraph () { + printGraph (output = value => console.log(value)) { const keys = Object.keys(this.adjacencyMap) for (const i of keys) { const values = this.adjacencyMap[i] let vertex = '' - for (const j of values) { vertex += j + ' ' } - console.log(i + ' -> ' + vertex) + for (const j of values) { + vertex += j + ' ' + } + output(i + ' -> ' + vertex) } } @@ -36,7 +38,7 @@ class Graph { * * @param {number} source The source vertex to start BFS. */ - bfs (source) { + bfs (source, output = value => console.log(value)) { const queue = [] const visited = new Set() queue.unshift([source, 0]) // level of source is 0 @@ -46,7 +48,7 @@ class Graph { const node = front[0] const level = front[1] queue.shift() // remove the front of the queue - console.log(`Visited node ${node} at level ${level}.`) + output(`Visited node ${node} at level ${level}.`) for (const next of this.adjacencyMap[node]) { if (!visited.has(next)) { // not visited queue.unshift([next, level + 1]) // level 1 more than current @@ -68,11 +70,12 @@ const example = () => { g.addEdge(1, 3) g.addEdge(2, 4) g.addEdge(2, 5) - console.log('Printing the adjacency list:\n') - g.printGraph() - // perform a breadth first search - console.log('\nBreadth first search at node 1:\n') + // Printing the adjacency list + // g.printGraph() + + // Breadth first search at node 1 g.bfs(1) } -example() + +export { Graph, example } diff --git a/Data-Structures/Heap/MaxHeap.js b/Data-Structures/Heap/MaxHeap.js index d6d849cbd..47f4c1b9e 100644 --- a/Data-Structures/Heap/MaxHeap.js +++ b/Data-Structures/Heap/MaxHeap.js @@ -71,15 +71,15 @@ class BinaryHeap { } } -const maxHeap = new BinaryHeap() -maxHeap.insert([4]) -maxHeap.insert([3]) -maxHeap.insert([6]) -maxHeap.insert([1]) -maxHeap.insert([8]) -maxHeap.insert([2]) +// Example -while (!maxHeap.empty()) { - const mx = maxHeap.extractMax() - console.log(mx) -} +// const maxHeap = new BinaryHeap() +// maxHeap.insert([4]) +// maxHeap.insert([3]) +// maxHeap.insert([6]) +// maxHeap.insert([1]) +// maxHeap.insert([8]) +// maxHeap.insert([2]) +// const mx = maxHeap.extractMax() + +export { BinaryHeap } diff --git a/Data-Structures/Linked-List/DoublyLinkedList.js b/Data-Structures/Linked-List/DoublyLinkedList.js index fdd8ea741..6614b3ba4 100644 --- a/Data-Structures/Linked-List/DoublyLinkedList.js +++ b/Data-Structures/Linked-List/DoublyLinkedList.js @@ -196,7 +196,11 @@ function DoubleLinkedList () { } } -const newDoubleLinkedList = new DoubleLinkedList() -newDoubleLinkedList.append(1) -newDoubleLinkedList.append(2) -console.log('Testing: ' + newDoubleLinkedList.size()) // returns 2 +// Example + +// const newDoubleLinkedList = new DoubleLinkedList() +// newDoubleLinkedList.append(1) +// newDoubleLinkedList.append(2) +// newDoubleLinkedList.size() // returns 2 + +export { DoubleLinkedList } diff --git a/String/CheckRearrangePalindrome.js b/String/CheckRearrangePalindrome.js index e4a8b6ca6..2f0e698ef 100644 --- a/String/CheckRearrangePalindrome.js +++ b/String/CheckRearrangePalindrome.js @@ -6,7 +6,7 @@ * **/ -const palindromeRearranging = (str) => { +export const palindromeRearranging = (str) => { // check that input is a string if (typeof str !== 'string') { return 'Not a string' @@ -27,5 +27,9 @@ const palindromeRearranging = (str) => { } // testing -console.log(palindromeRearranging('aaeccrr')) // true -console.log(palindromeRearranging('leve')) // false + +// > palindromeRearranging('aaeccrr') +// true + +// > palindromeRearranging('leve') +// false diff --git a/String/DiceCoefficient.js b/String/DiceCoefficient.js index b2f3a34be..ca9471159 100644 --- a/String/DiceCoefficient.js +++ b/String/DiceCoefficient.js @@ -44,8 +44,7 @@ function diceCoefficient (stringA, stringB) { // cut 0.xxxxxx to 0.xx for simplicity dice = Math.floor(dice * 100) / 100 - console.log('Dice coefficient of', stringA, 'and', stringB, 'is', dice) - return dice } + export { diceCoefficient } diff --git a/String/GenerateGUID.js b/String/GenerateGUID.js index 868819997..f30207382 100644 --- a/String/GenerateGUID.js +++ b/String/GenerateGUID.js @@ -4,7 +4,7 @@ The script uses `Math.random` in combination with the timestamp for better rando The function generate an RFC4122 (https://www.ietf.org/rfc/rfc4122.txt) version 4 UUID/GUID */ -const Guid = () => { +export const Guid = () => { const pattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' let currentDateMilliseconds = new Date().getTime() return pattern.replace(/[xy]/g, currentChar => { @@ -14,4 +14,5 @@ const Guid = () => { }) } -console.log(Guid()) // 'edc848db-3478-1760-8b55-7986003d895f' +// > Guid() +// 'edc848db-3478-1760-8b55-7986003d895f' diff --git a/String/LevenshteinDistance.js b/String/LevenshteinDistance.js index 428e71265..1b6ece70c 100644 --- a/String/LevenshteinDistance.js +++ b/String/LevenshteinDistance.js @@ -37,14 +37,6 @@ const levenshteinDistance = (a, b) => { } } - console.log( - 'Levenshtein Distance between ' + - a + - ' and ' + - b + - ' is = ' + - distanceMatrix[b.length][a.length] - ) return distanceMatrix[b.length][a.length] } diff --git a/Timing-Functions/IntervalTimer.js b/Timing-Functions/IntervalTimer.js index 2864a32c2..2eeed116e 100644 --- a/Timing-Functions/IntervalTimer.js +++ b/Timing-Functions/IntervalTimer.js @@ -63,7 +63,7 @@ class IntervalTimer { * Saturday, 01 August 2020 8:33 AM * @description Example usage */ -const ExampleIntervalTimer = function () { +const ExampleIntervalTimer = function (output = v => console.log(v)) { /** * Create am object with default settings. * @type {IntervalTimer} Used to get timing information. @@ -82,12 +82,12 @@ const ExampleIntervalTimer = function () { // ... A test ... // The time taken to run the test. - console.log(timer.getElapsedTime(initOffset)) + output(timer.getElapsedTime(initOffset)) /** * Returns the elapsed time and resets the timer to 0. */ - console.log(timer.resetTimer()) + output(timer.resetTimer()) } -ExampleIntervalTimer() +export { IntervalTimer, ExampleIntervalTimer }