Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8.2 follow up #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Chapter17-Moderate/swapNumbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Write a function to swap a number in place (that is, without any temporary variables).
*/

function switchVals(a,b) {
if (a > b) {
a = a - b;
b = a + b;
a = b - a;
} else if (b > a) {
b = b - a;
a = a + b;
b = a - b;
}
}
18 changes: 18 additions & 0 deletions Chapter18-Hard/18.1-addition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Write a function that adds two numbers. You should not use + or any arithmetic operators.
*/

//convert numbers to binary in order to perform operations on them
function add(num1, num2) {
let bin1 = num1.toString(2);
let bin2 = num2.toString(2);
return parseInt(addBinary(bin1, bin2), 2);
}

//recursively add binary numbers using XOR and AND
function addBinary(bin1, bin2) {
if (bin2 === 0) return bin1;
let sum = bin1 ^ bin2;
let carry = (bin1 & bin2) << 1;
return addBinary(sum, carry);
}
69 changes: 69 additions & 0 deletions Chapter3-StacksandQueues/3.7-AnimalShelter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Problem: An animal shelter holds only dogs and cats, and operates on a strictly 'first in, first out' basis. People must adopt either the 'oldest' (based on arrival time) of all animals at the shelter, or they can selected whether they would prefer a dog or a cat (and will receive the oldest anumal of that type). They cannot selected which specific animal they would like. Create the data structures to maintain this system and implement operations such as enqueue, dequeueAny, dequeueDog and dequeueCat.
*/
function Shelter(){
this.head = null;
this.tail = null;
}

Shelter.prototype.enqueue = function(type, name) {
var data = {
type: type,
name: name
};
if (!this.tail) {
var newNode = new Node(data, null, null);
this.head = newNode;
this.tail = newNode;
} else {
var currTail = this.tail;
var newTail = new Node(data, currTail, null);
currTail.next = newTail;
this.tail = newTail;
}
};

function Node(data, previous, next) {
this.data = data;
this.previous = previous;
this.next = next;
}

Shelter.prototype.dequeueAny = function () { //remove the tail and return its value
if (!this.tail)
return;
var value = this.tail.data;
this.tail = this.tail.previous;
if (this.tail) {
this.tail.next = null;
} else {
this.head = null;
}
return value;
};

Shelter.prototype.dequeueCat = function(){
if (!this.tail) return;
var last = this.tail;
while (last.data.type !== 'cat') {
last = last.previous;
}
var animal = last.data;
var temp = last.previous;
if (last.previous) last.previous.next = last.next;
last.next.previous = temp;
return animal;
}

Shelter.prototype.dequeueDog = function(){
if (!this.tail) return;
var last = this.tail;
while (last.data.type !== 'dog') {
last = last.previous;
}
var animal = last.data;
var temp = last.previous;
if (last.previous) last.previous.next = last.next;
last.next.previous = temp;
return animal;
}
53 changes: 52 additions & 1 deletion Chapter8-Recursion/8.2-GridPaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,55 @@ function memoize(n, xPos, yPos) {
return numPaths[[n,xPos,yPos]];
numPaths[[n,xPos,yPos]] = getPaths(n,xPos,yPos);
return numPaths[[n,xPos,yPos]];
}
}

/*
Follow Up: Imagine certain spots are 'off limits', such that the robot cannot step on them.
Design an algorithm to find a path for the robot from the top left to the bottom right;
*/

var findPath = function(x,y,forbiddenArray) {
var path = [];
//cache so that we do not iterate over the same point twice
var cache = {};

var isForbidden = function(x, y) {
var res = false;
for (var i = 0; i < forbiddenArray.length; i++) {
if (forbiddenArray[i][0] === x && forbiddenArray[i][1] === y) {
if (!cache[x]) cache[x] = {};
cache[x][y] = false;
res = true;
break;
} else {
if (!cache[x]) cache[x] = {};
cache[x][y] = true;
}
}
return res;
};

var existsPath = function(x,y,forbiddenArray) {
if (x < 0 || y < 0) return false;
if (cache[x] && cache[x][y]) return cache[x][y];
if (isForbidden(x,y)) return false;

if ((x ===0) && (y === 0)) {
path.push([0,0]);
return true;
}

if (existsPath(x-1, y, forbiddenArray) || existsPath(x, y-1, forbiddenArray)){
path.push([x,y]);
if (!cache[x]) cache[x] = {};
cache[x][y] = true;
return true;
}

return false;
};

existsPath(x,y,forbiddenArray);

return path;
};//runtime: O(x * y)