Skip to content

Commit

Permalink
Remove live code & console.log, leave examples as comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlte committed Oct 11, 2021
1 parent 90356f3 commit e18718b
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 59 deletions.
1 change: 0 additions & 1 deletion Backtracking/GeneratePermutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
4 changes: 2 additions & 2 deletions Backtracking/KnightTour.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions Backtracking/NQueen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions Backtracking/Sudoku.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
6 changes: 4 additions & 2 deletions Ciphers/KeywordShiftedAlphabet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!
5 changes: 1 addition & 4 deletions Conversions/BinaryToDecimal.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down
23 changes: 13 additions & 10 deletions Data-Structures/Graph/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 }
22 changes: 11 additions & 11 deletions Data-Structures/Heap/MaxHeap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
12 changes: 8 additions & 4 deletions Data-Structures/Linked-List/DoublyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
10 changes: 7 additions & 3 deletions String/CheckRearrangePalindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -27,5 +27,9 @@ const palindromeRearranging = (str) => {
}

// testing
console.log(palindromeRearranging('aaeccrr')) // true
console.log(palindromeRearranging('leve')) // false

// > palindromeRearranging('aaeccrr')
// true

// > palindromeRearranging('leve')
// false
3 changes: 1 addition & 2 deletions String/DiceCoefficient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
5 changes: 3 additions & 2 deletions String/GenerateGUID.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -14,4 +14,5 @@ const Guid = () => {
})
}

console.log(Guid()) // 'edc848db-3478-1760-8b55-7986003d895f'
// > Guid()
// 'edc848db-3478-1760-8b55-7986003d895f'
8 changes: 0 additions & 8 deletions String/LevenshteinDistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
8 changes: 4 additions & 4 deletions Timing-Functions/IntervalTimer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 }

0 comments on commit e18718b

Please sign in to comment.