Skip to content
Open

3.7 #14

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;
}