From 164eec6e5ca6d41e6d67e8923c8ab54bf9a3e20e Mon Sep 17 00:00:00 2001 From: Kamilla Date: Sat, 2 Jul 2016 19:38:55 -0400 Subject: [PATCH 01/11] 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 02/11] 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 e52c26a135423c37431b3847184fdf29f33206b1 Mon Sep 17 00:00:00 2001 From: Kamilla Date: Sun, 3 Jul 2016 11:34:16 -0400 Subject: [PATCH 03/11] solution to 1.6 --- Chapter1/1.6-MatrixRotation.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Chapter1/1.6-MatrixRotation.js diff --git a/Chapter1/1.6-MatrixRotation.js b/Chapter1/1.6-MatrixRotation.js new file mode 100644 index 0000000..6c631ec --- /dev/null +++ b/Chapter1/1.6-MatrixRotation.js @@ -0,0 +1,31 @@ +/* +Problem: Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, +write a method to rotate the image by 90 degrees. +Can you do this in place? +*/ + +/* +ex: input [[0,1,0], [0,1,0], [0,0,0]] returns [ [ 0, 0, 0 ], [ 0, 1, 1 ], [ 0, 0, 0 ] ] +*/ + +function rotateNinety(matrix) { + const N = matrix.length; + var floor = Math.floor(N/2); + var ceil = Math.ceil(N/2); + x = 0; + y = 0; + while (x < floor) { + while(y < ceil) { + let temp = matrix[x][y]; + matrix[x][y] = matrix[N-y-1][x]; + matrix[N-y-1][x] = matrix[N-x-1][N-y-1]; + matrix[N-x-1][N-y-1] = matrix[y][N - x-1]; + matrix[y][N - x-1] = temp; + y++; + } + x++; + } + return matrix; +} + +//runtime: O(n^2) \ No newline at end of file From 3603a3b89365693f12b704d201be87d4fa523d7c Mon Sep 17 00:00:00 2001 From: Kamilla Date: Sun, 3 Jul 2016 12:36:48 -0400 Subject: [PATCH 04/11] solution to 1.7 --- Chapter1/1.7-ClearMatrix.js | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Chapter1/1.7-ClearMatrix.js diff --git a/Chapter1/1.7-ClearMatrix.js b/Chapter1/1.7-ClearMatrix.js new file mode 100644 index 0000000..32a9a5a --- /dev/null +++ b/Chapter1/1.7-ClearMatrix.js @@ -0,0 +1,39 @@ +/* +Problem: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0. +*/ + +/* +ex: input [ [1 ,2, 3], [0, 0, 4], [5, 6, 7] ] returns [ [ 0, 0, 3 ], [ 0, 0, 0 ], [ 0, 0, 7 ] ] +*/ + +function clearZeroes(matrix) { + //create empty row to use later on + var emptyRow = matrix[0].map(function(){ + return 0; + }) + + //store columns to check if column has already been cleared + var emptyColumnArray = []; + + var emptyColumns = function(index) { + emptyColumnArray.push(index); + matrix.forEach(function(row, i) { + matrix[i][index] = 0; + }) + } + + matrix.forEach((row,j) => { + for (let i = 0, len = row.length; i < len; i++) { + if (row[i] === 0 && emptyColumnArray.indexOf(i) === -1) { + emptyColumns(i); + matrix[j] = emptyRow; + //break out of loop since we already cleared the entire row + i = -1; + } + } + }) + + return matrix; +} + +//runtime: O(n^2) From 2ded750e437fb73362066d0a9f2a5c2bb9403e60 Mon Sep 17 00:00:00 2001 From: Kamilla Date: Sun, 3 Jul 2016 21:18:10 -0400 Subject: [PATCH 05/11] solution to 2.5 --- Chapter2-LinkedLists/2.5-AddLinkedLists.js | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Chapter2-LinkedLists/2.5-AddLinkedLists.js diff --git a/Chapter2-LinkedLists/2.5-AddLinkedLists.js b/Chapter2-LinkedLists/2.5-AddLinkedLists.js new file mode 100644 index 0000000..20ac2ed --- /dev/null +++ b/Chapter2-LinkedLists/2.5-AddLinkedLists.js @@ -0,0 +1,24 @@ +/* +Problem: You have two numbers represented by a linked list, where each node contains a sinle digit. THe digits are stored in reverse order, such that the 1's digits is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list. +*/ + +function addLinkedList(list1, list2) { + let newList = new LinkedList(); + let num1 = list1.head; + let num2 = list2.head; + var carry = 0; + while ((num1 && num1.data) || (num2 && num2.data)) { + console.log(num1.data) + if (!num1) num1 = {data: 0}; + if (!num2) num2 = {data: 0}; + let sum = num1.data + num2.data + carry; + newList.insert(sum % 10) + carry = sum >= 10 ? 1 : 0; + num1 = num1.next; + num2 =num2.next; + } + if (carry === 1) { + newList.insert(1); + } + return newList; +} \ No newline at end of file From 30caf0c8898b31b102baf474f9d1a5f7aa6e6fa4 Mon Sep 17 00:00:00 2001 From: Kamilla Date: Mon, 4 Jul 2016 15:20:57 -0400 Subject: [PATCH 06/11] tower of hanoi --- Chapter3-StacksandQueues/3.4-TowersOfHanoi.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Chapter3-StacksandQueues/3.4-TowersOfHanoi.js diff --git a/Chapter3-StacksandQueues/3.4-TowersOfHanoi.js b/Chapter3-StacksandQueues/3.4-TowersOfHanoi.js new file mode 100644 index 0000000..a6f20f9 --- /dev/null +++ b/Chapter3-StacksandQueues/3.4-TowersOfHanoi.js @@ -0,0 +1,20 @@ +/* +Problem: In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sixez which can slide onto any tower. The puzzle starts with. The puzzle starts wtih disks sorted in ascending order of size from top to bottom (each disk sits on top of an even larger one). You have the following constraints: +1. Only one disk can be moved at a time. +2. A disk is slid off the top of one tower onto the next rod. +3. A disk can onle be placed on top of a larger disk. + +Write a prpogram to move the disks from the first to the last using Stacks. +*/ + +/** +@params: Three stacks, extends stack in data structure. +**/ +function moveDisks(origin, buffer, destination) { + while(origin.top) { + var top = origin.pop(); + moveDisks(origin, buffer, destination); + destination.push(top); + moveDisks(buffer, origin, destination); + } +} From 69d4d37dfdb8820ee789779f8f81db52e02c50d4 Mon Sep 17 00:00:00 2001 From: Kamilla Date: Mon, 4 Jul 2016 17:14:57 -0400 Subject: [PATCH 07/11] solution for 4.2 --- Chapter18-Hard/4.2-checkGraphRoute.js | 45 +++++++++++++++++++ Chapter3-StacksandQueues/3.4-TowersOfHanoi.js | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 Chapter18-Hard/4.2-checkGraphRoute.js diff --git a/Chapter18-Hard/4.2-checkGraphRoute.js b/Chapter18-Hard/4.2-checkGraphRoute.js new file mode 100644 index 0000000..1be4be3 --- /dev/null +++ b/Chapter18-Hard/4.2-checkGraphRoute.js @@ -0,0 +1,45 @@ +/* +Problem: Given a directed graph, design an algorithm to find out whether there is a route between two nodes. +*/ + +var Graph = function(){ + this.nodes = []; +} + +Graph.prototype.addNode = function(node){ + this.nodes.push(node); +} + +var GraphNode = function(data) { + this.data = data; + this.edges = []; +} + +GraphNode.prototype.addEdge = function(node) { + this.edges.push(node); +} + +Graph.prototype.checkConnection = function(startNode, endNode) { + var connected = false; + var graph = this; + + //reduce potential runtime by storing edges we've already checked + var checkedEdges = []; + var checkEdge = function(edge) { + if (connected !== false) return; + if (edge.data === endNode.data) { + connected = true; + return; + } + else if (edge.edges.length === 0 || checkedEdges.indexOf(edge.data) > -1) return; + else { + checkedEdges.push(edge.data); + return edge.edges.forEach(function(subEdge){ return checkEdge(subEdge)}) + } + } + checkEdge(startNode, endNode) + + return connected; +} + +//runtime: O(n) \ No newline at end of file diff --git a/Chapter3-StacksandQueues/3.4-TowersOfHanoi.js b/Chapter3-StacksandQueues/3.4-TowersOfHanoi.js index a6f20f9..d2bf4c8 100644 --- a/Chapter3-StacksandQueues/3.4-TowersOfHanoi.js +++ b/Chapter3-StacksandQueues/3.4-TowersOfHanoi.js @@ -4,7 +4,7 @@ Problem: In the classic problem of the Towers of Hanoi, you have 3 towers and N 2. A disk is slid off the top of one tower onto the next rod. 3. A disk can onle be placed on top of a larger disk. -Write a prpogram to move the disks from the first to the last using Stacks. +Write a program to move the disks from the first to the last using Stacks. */ /** From 13dcfdeb73add29d1416cc32bde633475dee303c Mon Sep 17 00:00:00 2001 From: Kamilla Date: Mon, 4 Jul 2016 18:01:31 -0400 Subject: [PATCH 08/11] switch 4.2 to correct folder --- {Chapter18-Hard => Chapter4-Trees&Graphs}/4.2-checkGraphRoute.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Chapter18-Hard => Chapter4-Trees&Graphs}/4.2-checkGraphRoute.js (100%) diff --git a/Chapter18-Hard/4.2-checkGraphRoute.js b/Chapter4-Trees&Graphs/4.2-checkGraphRoute.js similarity index 100% rename from Chapter18-Hard/4.2-checkGraphRoute.js rename to Chapter4-Trees&Graphs/4.2-checkGraphRoute.js From a00cc558f68278d7c40f4e5d99bdbe36fdcda307 Mon Sep 17 00:00:00 2001 From: Kamilla Date: Mon, 4 Jul 2016 18:07:39 -0400 Subject: [PATCH 09/11] solution to 5.2 --- .../5.2-decimalToBinary.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Chapter5-BitManipulation/5.2-decimalToBinary.js diff --git a/Chapter5-BitManipulation/5.2-decimalToBinary.js b/Chapter5-BitManipulation/5.2-decimalToBinary.js new file mode 100644 index 0000000..0031c8a --- /dev/null +++ b/Chapter5-BitManipulation/5.2-decimalToBinary.js @@ -0,0 +1,23 @@ +/** +@params(num) - decimal number +**/ + +var decimalToBinary = function(num) { + var string = '0.'; + var i = 1; + + while (num > 0) { + if (string.length > 32) return 'ERROR'; + if (num >= 1/Math.pow(2,i)) { + string += '1'; + num -= (1/Math.pow(2,i)); + } else { + string += '0'; + } + i++; + } + + return string; +}; + +//runtime: constant \ No newline at end of file From eabaaf4b7cead6cd203f7dc2c87f921bae6627bb Mon Sep 17 00:00:00 2001 From: Kamilla Date: Thu, 14 Jul 2016 19:39:43 -0400 Subject: [PATCH 10/11] solution 4.2 --- Chapter18-Hard/4.2-checkGraphRoute.js | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Chapter18-Hard/4.2-checkGraphRoute.js diff --git a/Chapter18-Hard/4.2-checkGraphRoute.js b/Chapter18-Hard/4.2-checkGraphRoute.js new file mode 100644 index 0000000..1be4be3 --- /dev/null +++ b/Chapter18-Hard/4.2-checkGraphRoute.js @@ -0,0 +1,45 @@ +/* +Problem: Given a directed graph, design an algorithm to find out whether there is a route between two nodes. +*/ + +var Graph = function(){ + this.nodes = []; +} + +Graph.prototype.addNode = function(node){ + this.nodes.push(node); +} + +var GraphNode = function(data) { + this.data = data; + this.edges = []; +} + +GraphNode.prototype.addEdge = function(node) { + this.edges.push(node); +} + +Graph.prototype.checkConnection = function(startNode, endNode) { + var connected = false; + var graph = this; + + //reduce potential runtime by storing edges we've already checked + var checkedEdges = []; + var checkEdge = function(edge) { + if (connected !== false) return; + if (edge.data === endNode.data) { + connected = true; + return; + } + else if (edge.edges.length === 0 || checkedEdges.indexOf(edge.data) > -1) return; + else { + checkedEdges.push(edge.data); + return edge.edges.forEach(function(subEdge){ return checkEdge(subEdge)}) + } + } + checkEdge(startNode, endNode) + + return connected; +} + +//runtime: O(n) \ No newline at end of file From c48a44735d3f6668cfca3efac8f8e01e081e46d3 Mon Sep 17 00:00:00 2001 From: Kamilla Date: Thu, 14 Jul 2016 20:42:34 -0400 Subject: [PATCH 11/11] solution to 4.3 --- Chapter4-Trees&Graphs/4.3-sortedArrayToBST.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Chapter4-Trees&Graphs/4.3-sortedArrayToBST.js diff --git a/Chapter4-Trees&Graphs/4.3-sortedArrayToBST.js b/Chapter4-Trees&Graphs/4.3-sortedArrayToBST.js new file mode 100644 index 0000000..3348e2d --- /dev/null +++ b/Chapter4-Trees&Graphs/4.3-sortedArrayToBST.js @@ -0,0 +1,17 @@ +/* +Problem: GIven a sorted (increasing oders) array with unique integer elements, write an algorithm to create a binary search tree with minimal height. +*/ + +//building off of the BST in Data Structures + +var createBST = function(arr){ + if (arr.length === 0) return null; + var midpoint = Math.floor(arr.length/2); + var tree = new BST(arr[midpoint]); + tree.left = createBST(arr.slice(0, midpoint)); + tree.right = createBST(arr.slice(midpoint + 1, arr.length)); + + return tree; +}; + +//runtime: O(n) \ No newline at end of file