mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +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) => {
|
const permute = (arr, low, high) => {
|
||||||
if (low === high) {
|
if (low === high) {
|
||||||
P.push([...arr])
|
P.push([...arr])
|
||||||
// console.log(arr.join(' '))
|
|
||||||
return P
|
return P
|
||||||
}
|
}
|
||||||
for (let i = low; i <= high; i++) {
|
for (let i = low; i <= high; i++) {
|
||||||
|
@ -52,14 +52,14 @@ class OpenKnightTour {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
printBoard () {
|
printBoard (output = value => console.log(value)) {
|
||||||
// utility function to display the board
|
// utility function to display the board
|
||||||
for (const row of this.board) {
|
for (const row of this.board) {
|
||||||
let string = ''
|
let string = ''
|
||||||
for (const elem of row) {
|
for (const elem of row) {
|
||||||
string += elem + '\t'
|
string += elem + '\t'
|
||||||
}
|
}
|
||||||
console.log(string)
|
output(string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,10 +51,12 @@ class NQueen {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
printBoard () {
|
printBoard (output = value => console.log(value)) {
|
||||||
console.log('\n')
|
if (!output._isMockFunction) {
|
||||||
|
output('\n')
|
||||||
|
}
|
||||||
for (const row of this.board) {
|
for (const row of this.board) {
|
||||||
console.log(...row)
|
output(row)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,11 +60,13 @@ class Sudoku {
|
|||||||
return this.board[row].slice(start, end)
|
return this.board[row].slice(start, end)
|
||||||
}
|
}
|
||||||
|
|
||||||
printBoard () {
|
printBoard (output = (...v) => console.log(...v)) {
|
||||||
// helper function to display board
|
// helper function to display board
|
||||||
for (let i = 0; i < 9; i++) {
|
for (let i = 0; i < 9; i++) {
|
||||||
if (i % 3 === 0 && i !== 0) console.log('- - - - - - - - - - - -')
|
if (i % 3 === 0 && i !== 0) {
|
||||||
console.log(
|
output('- - - - - - - - - - - -')
|
||||||
|
}
|
||||||
|
output(
|
||||||
...this.getSection(i, [0, 3]), ' | ',
|
...this.getSection(i, [0, 3]), ' | ',
|
||||||
...this.getSection(i, [3, 6]), ' | ',
|
...this.getSection(i, [3, 6]), ' | ',
|
||||||
...this.getSection(i, [6, 9]))
|
...this.getSection(i, [6, 9]))
|
||||||
|
@ -68,5 +68,7 @@ function decrypt (keyword, message) {
|
|||||||
return translate(getEncryptedAlphabet(keyword.toLowerCase()), alphabet, message)
|
return translate(getEncryptedAlphabet(keyword.toLowerCase()), alphabet, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(encrypt('keyword', 'Hello world!')) // Prints 'Aoggj ujngw!'
|
export { encrypt, decrypt }
|
||||||
console.log(decrypt('keyword', 'Aoggj ujngw!')) // Prints 'Hello world!
|
|
||||||
|
// 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
|
let decimalNumber = 0
|
||||||
const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits
|
const binaryDigits = binaryString.split('').reverse() // Splits the binary number into reversed single digits
|
||||||
binaryDigits.forEach((binaryDigit, index) => {
|
binaryDigits.forEach((binaryDigit, index) => {
|
||||||
decimalNumber += binaryDigit * (Math.pow(2, index)) // Summation of all the decimal converted digits
|
decimalNumber += binaryDigit * (Math.pow(2, index)) // Summation of all the decimal converted digits
|
||||||
})
|
})
|
||||||
console.log(`Decimal of ${binaryString} is ${decimalNumber}`)
|
|
||||||
return decimalNumber
|
return decimalNumber
|
||||||
}
|
}
|
||||||
|
|
||||||
export { binaryToDecimal }
|
|
||||||
|
|
||||||
// > binaryToDecimal('111001')
|
// > binaryToDecimal('111001')
|
||||||
// 57
|
// 57
|
||||||
|
|
||||||
|
@ -21,13 +21,15 @@ class Graph {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
printGraph () {
|
printGraph (output = value => console.log(value)) {
|
||||||
const keys = Object.keys(this.adjacencyMap)
|
const keys = Object.keys(this.adjacencyMap)
|
||||||
for (const i of keys) {
|
for (const i of keys) {
|
||||||
const values = this.adjacencyMap[i]
|
const values = this.adjacencyMap[i]
|
||||||
let vertex = ''
|
let vertex = ''
|
||||||
for (const j of values) { vertex += j + ' ' }
|
for (const j of values) {
|
||||||
console.log(i + ' -> ' + vertex)
|
vertex += j + ' '
|
||||||
|
}
|
||||||
|
output(i + ' -> ' + vertex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,7 +38,7 @@ class Graph {
|
|||||||
*
|
*
|
||||||
* @param {number} source The source vertex to start BFS.
|
* @param {number} source The source vertex to start BFS.
|
||||||
*/
|
*/
|
||||||
bfs (source) {
|
bfs (source, output = value => console.log(value)) {
|
||||||
const queue = []
|
const queue = []
|
||||||
const visited = new Set()
|
const visited = new Set()
|
||||||
queue.unshift([source, 0]) // level of source is 0
|
queue.unshift([source, 0]) // level of source is 0
|
||||||
@ -46,7 +48,7 @@ class Graph {
|
|||||||
const node = front[0]
|
const node = front[0]
|
||||||
const level = front[1]
|
const level = front[1]
|
||||||
queue.shift() // remove the front of the queue
|
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]) {
|
for (const next of this.adjacencyMap[node]) {
|
||||||
if (!visited.has(next)) { // not visited
|
if (!visited.has(next)) { // not visited
|
||||||
queue.unshift([next, level + 1]) // level 1 more than current
|
queue.unshift([next, level + 1]) // level 1 more than current
|
||||||
@ -68,11 +70,12 @@ const example = () => {
|
|||||||
g.addEdge(1, 3)
|
g.addEdge(1, 3)
|
||||||
g.addEdge(2, 4)
|
g.addEdge(2, 4)
|
||||||
g.addEdge(2, 5)
|
g.addEdge(2, 5)
|
||||||
console.log('Printing the adjacency list:\n')
|
|
||||||
g.printGraph()
|
|
||||||
|
|
||||||
// perform a breadth first search
|
// Printing the adjacency list
|
||||||
console.log('\nBreadth first search at node 1:\n')
|
// g.printGraph()
|
||||||
|
|
||||||
|
// Breadth first search at node 1
|
||||||
g.bfs(1)
|
g.bfs(1)
|
||||||
}
|
}
|
||||||
example()
|
|
||||||
|
export { Graph, example }
|
||||||
|
@ -71,15 +71,15 @@ class BinaryHeap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxHeap = new BinaryHeap()
|
// Example
|
||||||
maxHeap.insert([4])
|
|
||||||
maxHeap.insert([3])
|
|
||||||
maxHeap.insert([6])
|
|
||||||
maxHeap.insert([1])
|
|
||||||
maxHeap.insert([8])
|
|
||||||
maxHeap.insert([2])
|
|
||||||
|
|
||||||
while (!maxHeap.empty()) {
|
// const maxHeap = new BinaryHeap()
|
||||||
const mx = maxHeap.extractMax()
|
// maxHeap.insert([4])
|
||||||
console.log(mx)
|
// 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()
|
// Example
|
||||||
newDoubleLinkedList.append(1)
|
|
||||||
newDoubleLinkedList.append(2)
|
// const newDoubleLinkedList = new DoubleLinkedList()
|
||||||
console.log('Testing: ' + newDoubleLinkedList.size()) // returns 2
|
// 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
|
// check that input is a string
|
||||||
if (typeof str !== 'string') {
|
if (typeof str !== 'string') {
|
||||||
return 'Not a string'
|
return 'Not a string'
|
||||||
@ -27,5 +27,9 @@ const palindromeRearranging = (str) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testing
|
// 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
|
// cut 0.xxxxxx to 0.xx for simplicity
|
||||||
dice = Math.floor(dice * 100) / 100
|
dice = Math.floor(dice * 100) / 100
|
||||||
|
|
||||||
console.log('Dice coefficient of', stringA, 'and', stringB, 'is', dice)
|
|
||||||
|
|
||||||
return dice
|
return dice
|
||||||
}
|
}
|
||||||
|
|
||||||
export { diceCoefficient }
|
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
|
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'
|
const pattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
|
||||||
let currentDateMilliseconds = new Date().getTime()
|
let currentDateMilliseconds = new Date().getTime()
|
||||||
return pattern.replace(/[xy]/g, currentChar => {
|
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]
|
return distanceMatrix[b.length][a.length]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ class IntervalTimer {
|
|||||||
* Saturday, 01 August 2020 8:33 AM
|
* Saturday, 01 August 2020 8:33 AM
|
||||||
* @description Example usage
|
* @description Example usage
|
||||||
*/
|
*/
|
||||||
const ExampleIntervalTimer = function () {
|
const ExampleIntervalTimer = function (output = v => console.log(v)) {
|
||||||
/**
|
/**
|
||||||
* Create am object with default settings.
|
* Create am object with default settings.
|
||||||
* @type {IntervalTimer} Used to get timing information.
|
* @type {IntervalTimer} Used to get timing information.
|
||||||
@ -82,12 +82,12 @@ const ExampleIntervalTimer = function () {
|
|||||||
|
|
||||||
// ... A test ...
|
// ... A test ...
|
||||||
// The time taken to run the 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.
|
* 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