Skip to content

Instantly share code, notes, and snippets.

View iJustErikk's full-sized avatar

Erik Awwad iJustErikk

View GitHub Profile
@iJustErikk
iJustErikk / naivesubstring.py
Created November 24, 2020 19:02
Naive Substring Algorithm
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
// 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;
def naive_fibbo(n):
if (n <= 2): return 1
return naive_fibbo(n-1) + naive_fibbo(n-2)
// 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));
}
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));
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
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));
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));
// 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;
}
@iJustErikk
iJustErikk / cansum.js
Last active December 5, 2020 02:12
forgot to memoize false values
// 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;