mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
Remove live code & console.log, leave examples as comments.
This commit is contained in:
@ -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++) {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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]))
|
||||
|
@ -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!
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 }
|
||||
|
@ -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 }
|
||||
|
@ -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 }
|
||||
|
@ -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
|
||||
|
@ -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 }
|
||||
|
@ -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'
|
||||
|
@ -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]
|
||||
}
|
||||
|
||||
|
@ -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 }
|
||||
|
Reference in New Issue
Block a user