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
def indexOf(text, string): | |
for i in range(len(text)): | |
didBreak = False | |
for j in range(len(string)): | |
if (i+j >= len(text)): | |
didBreak = True | |
break | |
if (text[i + j] != string[j]): | |
didBreak = True | |
break |
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
// recursively divides then merges | |
const mergeSort = (curSubArr) => { | |
if (curSubArr.length <= 1) return curSubArr; | |
const middle = Math.floor(curSubArr.length / 2); | |
const left = curSubArr.slice(0, middle); | |
const right = curSubArr.slice(middle); | |
return merge(mergeSort(left), mergeSort(right)); | |
}; | |
const merge = (left, right) => { | |
let pointerA = 0; |
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
def naive_fibbo(n): | |
if (n <= 2): return 1 | |
return naive_fibbo(n-1) + naive_fibbo(n-2) |
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
// map supports has(key), get(key) and set(key, val) | |
const fib = (n, memo = new Map()) => { | |
if (memo.has(n)) return memo.get(n); | |
if (n <= 1) return 1; | |
memo.set(n, fib(n-1, memo) + fib(n-2, memo)); | |
return memo.get(n); | |
}; | |
for(let i = 0; i <= 1476; i++) { | |
console.log(i, fib(i)); | |
} |
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
const gridTraveler = (m, n) => { | |
if (m === 0 || n === 0) return 0; | |
if (m === 1 && n === 1) return 1; | |
return gridTraveler(m - 1, n) + gridTraveler(m, n - 1); | |
}; | |
console.log(gridTraveler(15, 15)); |
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
const spaceTraveler = (x, y, z) => { | |
if (x === 0 || y === 0 || z === 0) return 0; | |
if (x === 1 && y === 1 && z === 1) return 1; | |
return spaceTraveler(x - 1, y, z) + spaceTraveler(x, y - 1, z) + spaceTraveler(x, y, z - 1); | |
}; | |
console.log(gridTraveler(4, 4, 4)); | |
// you are traveling in space | |
// you are at (0, 0, 0) | |
// find how many ways you can get to (x, y, z) by only increasing to them | |
// x,y,z > 0, are integers |
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
const gridTraveler = (m, n, memo = new Map()) => { | |
const key = `${m},${n}`; | |
if (memo.has(key)) return memo.get(key); | |
if (m === 0 || n === 0) return 0; | |
if (m === 1 && n === 1) return 1; | |
memo.set(key, gridTraveler(m - 1, n, memo) + gridTraveler(m, n - 1, memo)); | |
return memo.get(key); | |
}; | |
console.log(gridTraveler(20, 20)); |
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
const gridTraveler = (x, y, z, memo = new Map()) => { | |
const key = `${x},${y},${z}`; | |
if (memo.has(key)) return memo.get(key); | |
if (x === 0 || y === 0 || z === 0) return 0; | |
if (x === 1 && y === 1 && z === 1) return 1; | |
memo.set(key, gridTraveler(x - 1, y, z, memo) + gridTraveler(x, y - 1, z, memo)) + gridTraveler(x, y, z - 1, memo)); | |
return memo.get(key); | |
}; | |
console.log(gridTraveler(10, 10, 10)); |
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
// t/f | |
// use number from numbers as many times as needed to construct targetSum | |
const canSum = (targetSum, numbers) => { | |
if (targetSum === 0) return true; | |
for(let i = 0; i < numbers.length; i += 1) { | |
const currentNum = numbers[i]; | |
const after = targetSum - currentNum; | |
if (after < 0) continue; | |
if(canSum(after, numbers)) return true; | |
} |
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
// t/f | |
// use number from numbers as many times as needed to construct targetSum | |
const canSum = (targetSum, numbers, memo = new Map()) => { | |
const key = `${targetSum}`; | |
if (targetSum === 0) return true; | |
if (memo.has(key)) return memo.get(key); | |
for (let currentNum of numbers) { | |
const after = targetSum - currentNum; | |
if (after < 0) continue; |
OlderNewer