From 164eec6e5ca6d41e6d67e8923c8ab54bf9a3e20e Mon Sep 17 00:00:00 2001 From: Kamilla Date: Sat, 2 Jul 2016 19:38:55 -0400 Subject: [PATCH 1/3] problem 17.1 --- Chapter17-Moderate/swapNumbers.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Chapter17-Moderate/swapNumbers.js 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 From b7d3bfc7a74e5fe0a07cc29af0d90ce2e36da1ad Mon Sep 17 00:00:00 2001 From: Kamilla Date: Sat, 2 Jul 2016 20:19:03 -0400 Subject: [PATCH 2/3] problem 18.1 --- Chapter18-Hard/18.1-addition.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Chapter18-Hard/18.1-addition.js 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 From 9ade5757db7b4faaaa7ee7285c19c5892d7abff8 Mon Sep 17 00:00:00 2001 From: KamillaKhabibrakhmanova Date: Fri, 29 Jul 2016 12:47:53 -0500 Subject: [PATCH 3/3] solution to 3.7 --- Chapter3-StacksandQueues/3.7-AnimalShelter.js | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Chapter3-StacksandQueues/3.7-AnimalShelter.js 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