diff --git a/Chapter17-Moderate/swapNumbers.js b/Chapter17-Moderate/swapNumbers.js new file mode 100644 index 0000000..e5b9235 --- /dev/null +++ b/Chapter17-Moderate/swapNumbers.js @@ -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; + } +} \ No newline at end of file diff --git a/Chapter18-Hard/18.1-addition.js b/Chapter18-Hard/18.1-addition.js new file mode 100644 index 0000000..157b0cf --- /dev/null +++ b/Chapter18-Hard/18.1-addition.js @@ -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); +} \ No newline at end of file diff --git a/Chapter3-StacksandQueues/3.7-AnimalShelter.js b/Chapter3-StacksandQueues/3.7-AnimalShelter.js new file mode 100644 index 0000000..166f945 --- /dev/null +++ b/Chapter3-StacksandQueues/3.7-AnimalShelter.js @@ -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; +} \ No newline at end of file