Remove live code & console.log, leave examples as comments (ProjectEuler, Recursive).

This commit is contained in:
Eric Lavault
2021-10-10 18:18:25 +02:00
parent 9212e6d684
commit 5f45b540b9
17 changed files with 48 additions and 98 deletions

View File

@ -13,14 +13,9 @@
*
*/
const binaryEquivalent = (num) => {
export const binaryEquivalent = (num) => {
if (num === 0 || num === 1) {
return String(num)
}
return binaryEquivalent(Math.floor(num / 2)) + String(num % 2)
}
// Driver Code
const num = 6
const ans = binaryEquivalent(num)
console.log(ans)

View File

@ -2,7 +2,7 @@
// https://en.wikipedia.org/wiki/Binary_search_algorithm
// Search the integer inside the sorted integers array using Binary Search Algorithm
const BinarySearch = (intArr, searchQuery) => {
export const BinarySearch = (intArr, searchQuery) => {
if (searchQuery === null || searchQuery === undefined || intArr.length === 0) {
return false
}
@ -17,13 +17,3 @@ const BinarySearch = (intArr, searchQuery) => {
return false
}
}
// testing
(() => {
console.log('Number Present with odd array length: 5 = ', BinarySearch([1, 2, 3, 4, 5, 6, 7], 5))
console.log('Number Present with even array length: 5 = ', BinarySearch([1, 2, 4, 5, 6], 5))
console.log('Number Present with only single element: 5 = ', BinarySearch([5], 5))
console.log('Number Not Present: 0 = ', BinarySearch([1, 2, 3, 4, 5], 0))
console.log('Undefined number search query = ', BinarySearch([1, 2, 3, 4, 5]))
console.log('With Empty array = ', BinarySearch([], 1))
})()

View File

@ -27,11 +27,4 @@ function euclideanGCDIterative (first, second) {
return first
}
function main () {
const first = 20
const second = 30
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second))
console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second))
}
main()
export { euclideanGCDIterative, euclideanGCDRecursive }

View File

@ -1,13 +1,15 @@
// https://en.wikipedia.org/wiki/Fibonacci_number
const fibonacci = (N) => {
if (N === 0 || N === 1) return N
/**
* Return the N-th Fibonacci number
*
* @param {number} N
* @returns {number}
*/
export const fibonacci = (N) => {
if (N === 0 || N === 1) {
return N
}
return fibonacci(N - 2) + fibonacci(N - 1)
}
// testing
(() => {
const number = 5
console.log(number + 'th Fibonacci number is ' + fibonacci(number))
})()

View File

@ -1,6 +1,6 @@
// Check whether the given string is Palindrome or not
const Palindrome = (str) => {
export const Palindrome = (str) => {
if (typeof str !== 'string') {
str = str.toString()
}
@ -18,13 +18,4 @@ const Palindrome = (str) => {
} else {
return Palindrome(str.slice(1, str.length - 1))
}
};
// testing
(() => {
console.log('Palindrome: String: a = ', Palindrome('a'))
console.log('Palindrome: String: abba = ', Palindrome('abba'))
console.log('Palindrome: String: ababa = ', Palindrome('ababa'))
console.log('Not Palindrome: String: abbxa = ', Palindrome('abbxa'))
console.log('Not Palindrome: String: abxa = ', Palindrome('abxa'))
})()
}

View File

@ -19,14 +19,12 @@
* https://en.wikipedia.org/wiki/Lexicographic_order
*/
const subsequence = (str, seq, low) => {
export const subsequence = (str, seq, low, output = []) => {
if (low <= str.length && str.length !== 0) {
console.log(seq)
output.push(seq)
}
for (let i = low; i < str.length; i++) {
subsequence(str, seq + str[i], i + 1)
subsequence(str, seq + str[i], i + 1, output)
}
return output
}
const str = 'abcd'
subsequence(str, '', 0)

View File

@ -1,16 +1,18 @@
// wiki - https://en.wikipedia.org/wiki/Tower_of_Hanoi
// Recursive Javascript function to solve tower of hanoi
function TowerOfHanoi (n, fromRod, toRod, auxRod) {
export function TowerOfHanoi (n, from, to, aux, output = []) {
if (n === 1) {
console.log(`Move disk 1 from rod ${fromRod} to rod ${toRod}`)
return
output.push(`Move disk 1 from rod ${from} to rod ${to}`)
return output
}
TowerOfHanoi(n - 1, fromRod, auxRod, toRod)
console.log(`Move disk ${n} from rod ${fromRod} to rod ${toRod}`)
TowerOfHanoi(n - 1, auxRod, toRod, fromRod)
TowerOfHanoi(n - 1, from, aux, to, output)
output.push(`Move disk ${n} from rod ${from} to rod ${to}`)
TowerOfHanoi(n - 1, aux, to, from, output)
return output
}
// Driver code
const n = 4
TowerOfHanoi(n, 'A', 'C', 'B')
// A, C, B are the name of rods
// Driver code (A, C, B are the name of rods)
// const n = 4
// TowerOfHanoi(n, 'A', 'C', 'B')

View File

@ -3,14 +3,9 @@
// 5! = 1*2*3*4*5 = 120
// 2! = 1*2 = 2
const factorial = (n) => {
export const factorial = (n) => {
if (n === 0) {
return 1
}
return n * factorial(n - 1)
}
// testing
console.log(factorial(4))
console.log(factorial(15))
console.log(factorial(0))