Created
July 3, 2019 12:24
-
-
Save elchroy/13163f6467a89c4ebf0141ced2b7eb26 to your computer and use it in GitHub Desktop.
Factorial, Fibonacci, Search Algorithms, etc. See sorting.js for sorting algorithms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
isUnique (arr) { | |
let result = true; | |
let count = 0; | |
for (let i = 0; i < arr.length; i++) { | |
console.log(`OUTER LOOP ~~~~ i === ${i}`); | |
for (let j = 0; j < arr.length; j++) { | |
console.log(`INNER LOOP ~~~~ j ==== ${j}, and ${++count}`); | |
// apart from the current index where i and j are equal | |
// and if the same number appears in difference indices, | |
// return false | |
if (i !== j && arr[i] === arr[j]) { | |
return false | |
result = false; | |
} | |
} | |
} | |
return result | |
}, | |
isUniqueWithCache (arr) { | |
const cache = {} | |
let count = 0; | |
let result = false; | |
for (var i = 0; i < arr.length; i++) { | |
console.log(`LOOP ~~~~~~ i === ${i}, and ${++count}`) | |
// if we've saved the item before, | |
// then the array is not unique, return false | |
if (cache[arr[i]]) { | |
result = false | |
return false | |
} else { | |
cache[arr[i]] = true | |
} | |
} | |
return result | |
}, | |
// Faster but takes up more space | |
myUniqSort_ (arr) { | |
const cache = {} | |
arr = arr.filter((e, i) => { | |
if (cache[e]) { | |
return false | |
} else { | |
cache[e] = true | |
return true | |
} | |
}) | |
return arr.sort((a, b) => a - b) | |
}, | |
uniqSort (arr) { | |
const cache = {}; | |
const result = []; | |
for (let i = 0; i < arr.length; i++) { | |
if (!cache[arr[i]]) { | |
result.push(arr[i]) | |
cache[arr[i]] = true | |
} | |
} | |
return result.sort((a, b) => a - b) | |
}, | |
factorialWithRecursion (num) { | |
return num < 2 ? 1 : num * this.factorialWithRecursion(num-1) | |
}, | |
factorialWithReduce (num) { | |
return [...Array(num+1).keys()].splice(1).reduce((x, y) => x*y, 1); | |
}, | |
factorialWithLoops (num) { | |
var result = 1; | |
for (let i = 1; i <= num; i++) { | |
result *= i | |
} | |
return result | |
}, | |
/** | |
* Find the product between a range of numbers | |
* from a start, to a stop value, separated by a step | |
* @param {[type]} start [description] | |
* @param {[type]} end [description] | |
* @param {Number} step [description] | |
* @return {[type]} [description] | |
*/ | |
rangeProductWithLoop (start, end, step=1) { | |
var result = 1; | |
for (let i = start; i <= end; i+=step) { | |
result *= i | |
} | |
return result | |
}, | |
loopNTimes (n) { | |
console.log(`n === ${n}`); | |
if (n <= 1) { | |
return 'complete' | |
} | |
return loopNTimes(n-1); | |
}, | |
loggingNumberWithLoops (start, end) { | |
console.log(`using a loop to log numbers from ${start} to ${end}`) | |
for (let i = start; i <= end; i++) { | |
console.log(`hitting index ${i}`) | |
} | |
}, | |
logNumbersRecursively (start, end) { | |
console.log(`recursively logging from ${start} to ${end}`) | |
function recurse (i) { | |
console.log(`hitting index ${i}`) | |
if (i < end) recurse(i+1) | |
} | |
recurse(start) | |
}, | |
// Wrapper Function technique for loops | |
MemoFnLoop(i, end) { | |
console.log(`looping from ${i} intil ${end}`) | |
if (i < end) this.MemoFnLoop(i+1, end) | |
}, | |
// this is using the accumulator technique | |
joinElements (array, joinString) { | |
function recurse (index, resultSoFar) { | |
resultSoFar += array[index] | |
return (index === array.length - 1) | |
? resultSoFar | |
: recurse(index+1, resultSoFar + joinString) | |
// this is where the accumulation is happening | |
} | |
return recurse(0, '') | |
}, | |
joinElementsIteratively (array, joinString) { | |
let result = ''; | |
let array_len = array.length | |
for (let i = 0; i < array_len-1; i++) { | |
result += array[i] + joinString | |
} | |
console.log(result + array[array_len-1]) | |
}, | |
memoizeFactorialWithRecursion (num) { | |
const cache = {}; | |
return function calc (n) { | |
if (cache[n]) { | |
// console.log(`getting result of ${n} from cache...`) | |
return cache[n]; | |
} else if (n < 2) { | |
// console.log('getting for 0 or 1...') | |
return 1; | |
} else { | |
// console.log(`calculating for ${n}, and storing in cache...`) | |
let result = n * calc(n - 1); | |
cache[n] = result | |
// console.log('calculating and storing in cache') | |
console.log(cache, 'here') | |
return result | |
} | |
} | |
}, | |
memoizeRecursiveFactorialCB () { | |
return this.memoize(this.factorialWithRecursion.bind(this)) | |
}, | |
memoize (fn) { | |
let cache = {} | |
return (...args) => { | |
let n = args[0] // consider using JSON.stringify here | |
if (cache[n]) { | |
return cache[n] | |
} else { | |
let result = fn(n) | |
cache[n] = result | |
return result | |
} | |
} | |
}, | |
// DIVIDE & CONQUER | |
// SEARCHing ALGORITHMS | |
linearSearchWithFind (list, item) { | |
// here we can also use filter, foreach | |
// with the zero index | |
return list.find(e => e === item) | |
}, | |
linearSearchWithFindIndex (list, item) { | |
return list.findIndex(e => e === item) | |
}, | |
linearSearchWithForEach (list, item) { | |
let index = -1 // not found | |
list.forEach( (e, i) => { | |
if (e === item) { | |
index = i | |
// this will return the last index | |
// if there is a duplicate | |
} | |
}) | |
return index | |
}, | |
// here we're looking for the index | |
binarySearchWithWhile (list, item) { | |
let min = 0; | |
let max = list.length - 1 | |
var guess; | |
while (min <= max) { | |
// start at the middle | |
guess = Math.floor((min+max)/2) | |
if (list[guess] === item) { | |
return guess | |
} else if (list[guess] < item) { | |
// move min to the right | |
min = guess + 1 | |
} else { | |
// move max to the left | |
max = guess - 1 | |
} | |
} | |
return -1 | |
}, | |
binarySearchWithRecursion (list, min, max, item) { | |
min = min || 0 | |
max = max || list.length-1 | |
let guess = Math.floor((min+max)/2); | |
console.log(list, `Focus Range - from ${min} to ${max}`) | |
if (list[guess] === item) { | |
return guess | |
} else if ((list[min] > item) || (list[max] < item)) { | |
console.log('new min is more that item or new max is less than item') | |
return -1 | |
} else { | |
if (min < max) { | |
if (list[guess] < item) { | |
console.log(`${list[guess]} is smaller than ${item}`) | |
return this.binarySearchWithRecursion(list, item, guess+1, max) | |
} else { | |
console.log(`${list[guess]} is larger than ${item}`) | |
return this.binarySearchWithRecursion(list, item, min, guess-1) | |
} | |
} | |
return -1 | |
} | |
}, | |
finbonacciSeqWithLoop (num) { | |
const result = [] | |
for (var i = 0; i < num; i++) { | |
e = i < 2 ? i : result[i-1] + result[i-2] | |
result.push(e) | |
} | |
return result | |
}, | |
finbonacciSeqWithRecursion (num) { | |
}, | |
finbonacciRatioWithLoop (num) { | |
}, | |
finbonacciNumWithRecursion (num) { | |
num = num === 0 ? 1 : num // start from 1 | |
return num <=2 | |
? num-1 | |
: this.finbonacciNumWithRecursion(num-1) | |
+ this.finbonacciNumWithRecursion(num-2) | |
}, | |
triangularNumSeries (num) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment