Skip to content

Instantly share code, notes, and snippets.

View verdi327's full-sized avatar
💭
Thinkful EI

Michael Verdi verdi327

💭
Thinkful EI
View GitHub Profile
@verdi327
verdi327 / suduko-solver.js
Created September 26, 2019 17:48
suduko-solver.js
var solveSudoku = function(board, row=0, col=0) {
function isSafe(row, col, num) {
return isRowSafe(row, num) && isColSafe(col, num) && isGridSafe(row, col, num);
}
function isRowSafe(row, num) {
for (let col=0; col<board.length;col++) {
if (board[row][col] === num) return false;
}
return true;
@verdi327
verdi327 / findBestStartingSpot.js
Created September 21, 2019 04:35
find-best-starting-position
function findBestStartingSpot(matrix) {
let bestStartingSpot = [];
let min = Infinity;
function validMove(row, col, matrixDup) {
return row >= 0 &&
row < matrixDup.length &&
col >= 0 &&
col < matrixDup[0].length &&
matrixDup[row][col] !== '*';
@verdi327
verdi327 / hotel-booking.js
Created September 20, 2019 03:53
hotel-booking
class Hotel {
constructor(numRooms) {
this.totalRooms = numRooms;
this.reservations = {};
}
book(startDay, endDay) {
let checkIn = this._keyify(startDay);
// do we have any reservations at this date
if (this.reservations[checkIn] && this.reservations[checkIn].length) {
@verdi327
verdi327 / encode-json.js
Created September 12, 2019 20:38
encode-json
function stringify(input) {
if (!input) return "null";
if (typeof(input) === 'string') return input;
if (typeof(input) === 'number') return `"${input}"`;
if (Array.isArray(input)) {
let result = [];
input.forEach(value => {
result.push(stringify(value));
});
return `[${result}]`;
@verdi327
verdi327 / graph-coloring.js
Created August 1, 2019 17:49
graph-coloring
class GraphNode {
constructor(label) {
this.label = label;
this.neighbors = new Set();
this.color = null;
}
}
const nodeA = new GraphNode('A');
const nodeB = new GraphNode('B');
@verdi327
verdi327 / binary-search-recursive.js
Created July 2, 2019 14:57
binary-search-recursive.js
function binarySearch(sortedArray, searchValue, minIndex=0, maxIndex=sortedArray.length-1) {
let currentElement, middleIndex;
while (minIndex <= maxIndex) {
// Find the value of the middle of the array
middleIndex = Math.floor((minIndex + maxIndex) / 2);
currentElement = sortedArray[middleIndex];
// It's the same as the value in the middle - we can return!
if (currentElement === searchValue) {
@verdi327
verdi327 / dfs-postorder.js
Created July 2, 2019 14:46
dfs-postorder.js
postOrder(result=[]) {
if (!this.left && !this.right) {
return result.push(this.key);
}
if (this.left) {
this.left.postOrder(result);
}
if (this.right) {
this.right.postOrder(result);
@verdi327
verdi327 / dfs-inorder.js
Created July 2, 2019 14:45
dfs-inorder.js
inOrder(result=[]) {
if (!this.left && !this.right) {
return result.push(this.key);
}
if (this.left) {
this.left.inOrder(result);
}
result.push(this.key);
if (this.right) {
this.right.inOrder(result);
@verdi327
verdi327 / dfs-preorder.js
Created July 2, 2019 14:43
dfs-preorder.js
preOrder(result=[]) {
if (!this.left && !this.right) {
return result.push(this.key);
}
result.push(this.key);
if (this.left) {
this.left.preOrder(result);
}
if (this.right) {
this.right.preOrder(result);
@verdi327
verdi327 / breadth-first-search
Created July 2, 2019 14:42
Breath First Search