From eff33ab9901d78dbe193a8bfb868266ed9364277 Mon Sep 17 00:00:00 2001 From: Claire Hsu Date: Sun, 13 Nov 2016 13:56:40 -0800 Subject: [PATCH 1/8] add solutions for balancedParens.js and maxQueue.js --- src/balancedParens.js | 40 +++++++++++++++++++++++++++++++++++++++- src/maxQueue.js | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/balancedParens.js b/src/balancedParens.js index d687dec..029911b 100755 --- a/src/balancedParens.js +++ b/src/balancedParens.js @@ -1,8 +1,46 @@ class Stack { // Your code here + constructor() { + this.items = []; + this.count = 0; + } + + getLength() { + return this.count; + } + + push(item) { + this.items.push(item); + this.count = this.count + 1; + } + + pop() { + if(this.count > 0) { + this.count = this.count - 1; + } + + return this.items.pop(); + } + + peek() { + return this.items.slice(-1)[0]; + } }; const balancedParens = (str) => { // Your code here - + let stack = new Stack(); + let open = { '{': '}', '[': ']', '(': ')' }; + let closed = { '}': true, ']': true, ')': true }; + + for (var i = 0; i < str.length; i ++) { + let chr = str[i]; + if (open[chr]) { + stack.push(chr); + } else if (closed[chr]) { + let popped = open[stack.pop()]; + if (popped !== chr) return false; + } + } + return stack.count === 0; }; diff --git a/src/maxQueue.js b/src/maxQueue.js index 04dbfd2..ddee8e0 100644 --- a/src/maxQueue.js +++ b/src/maxQueue.js @@ -1,4 +1,44 @@ class Queue { // Your code here - + constructor() { + this.items = []; + this.count = 0; + this.max = []; + } + + getLength() { + return this.count; + } + + enqueue(item) { + this.items.unshift(item); + this.count = this.count + 1; + if(this.max.length === 0 || this.max[0] <= item){ + this.max.unshift(item); + } + } + + dequeue() { + if(this.count > 0) { + this.count = this.count - 1; + } + var item = this.items.shift(); + if(item === this.max[0]){ + this.max.shift(); + } + if(this.max.length === 0 ){ + var tempMax; + for(var i = 0; i < this.items.length; i++){ + if(tempMax < this.items[i]){ + tempMax = this.item[i]; + } + } + this.max[0] = tempMax; + } + return item; + } + + peek() { + return this.items.slice(0,1)[0]; + } }; From c9fc48c5cc657119b8a9f2255891c968065f28de Mon Sep 17 00:00:00 2001 From: Claire Hsu Date: Sun, 13 Nov 2016 14:08:36 -0800 Subject: [PATCH 2/8] Update README.md --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a6effa9..e1b9877 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,16 @@ ## Introduction to Data Structures +Welcome to the wonderful world of data structures! If this is your first exposure to the subject, check out this [gentle introduction](http://blog.benoitvallon.com/data-structures-in-javascript/data-structures-in-javascript/). + ## Implementation +The first step to understanding data structures is learning how to build them, and what the advantages of each type are. For more on this topic, check out an explanantion of [stack](http://blog.benoitvallon.com/data-structures-in-javascript/the-stack-data-structure/) and [queue](http://blog.benoitvallon.com/data-structures-in-javascript/the-queue-data-structure/). + ### Classic Questions -## Reimplementation(Using Other Data Structures) +Once you feel like you've got a solid grasp of how these data structures work, go ahead and clone and take the repo for a whirl and take a stab at two classic interview questions making use of the stack and queue data structures, respectively. + +## Reimplementation (Using Other Data Structures) + +Once you've done that level up by reimplementing these data structures using the other data structure! From fae368d141d5282ee11eb3d9ea1a6edf6395d820 Mon Sep 17 00:00:00 2001 From: Claire Hsu Date: Sun, 13 Nov 2016 22:46:30 -0800 Subject: [PATCH 3/8] implement and test getMax --- lib/spec.js | 2 +- src/maxQueue.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/spec.js b/lib/spec.js index d7d6428..aea9076 100755 --- a/lib/spec.js +++ b/lib/spec.js @@ -21,7 +21,7 @@ describe('MaxQueue', function () { it('should return max item', function() { let queue = new Queue(); queue.enqueue(1) - expect(queue.max[0]).to.equal(1); + expect(queue.getMax()).to.equal(1); }); it('should remove items', function() { diff --git a/src/maxQueue.js b/src/maxQueue.js index ddee8e0..92e44de 100644 --- a/src/maxQueue.js +++ b/src/maxQueue.js @@ -41,4 +41,8 @@ class Queue { peek() { return this.items.slice(0,1)[0]; } + + getMax() { + return this.max[0]; + } }; From ab42301c079004d8ec1c2ec55480a2af43926399 Mon Sep 17 00:00:00 2001 From: ClarabelleCheng-Yue Date: Mon, 14 Nov 2016 20:51:04 -0800 Subject: [PATCH 4/8] implement min queue --- src/minQueue.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/minQueue.js diff --git a/src/minQueue.js b/src/minQueue.js new file mode 100644 index 0000000..05a7c81 --- /dev/null +++ b/src/minQueue.js @@ -0,0 +1,42 @@ +'use strict'; // this line is required because ES6 classes are written in strict mode. + +class Queue { + // Your code here + constructor() { + this.items = []; + this.count = 0; + this.min = []; + } + + getLength() { + return this.count; + } + + enqueue(item) { + this.items.unshift(item); + this.count = this.count + 1; + if(this.min.length === 0 || this.min[0] >= item){ + this.min.unshift(item); + } + } + + dequeue() { + if(this.count > 0) { + this.count = this.count - 1; + } + var item = this.items.shift(); + if(item === this.min[0]){ + this.min.shift(); + } + return item; + } + + peek() { + return this.items.slice(0,1)[0]; + } + + getMin() { + return this.min[0]; + } +}; + From ce9121605e456dddbe617113c5347062c9c0072a Mon Sep 17 00:00:00 2001 From: ClarabelleCheng-Yue Date: Mon, 14 Nov 2016 21:16:59 -0800 Subject: [PATCH 5/8] Queue class using private variables --- src/minQueue_privateVar.js | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/minQueue_privateVar.js diff --git a/src/minQueue_privateVar.js b/src/minQueue_privateVar.js new file mode 100644 index 0000000..c79574b --- /dev/null +++ b/src/minQueue_privateVar.js @@ -0,0 +1,89 @@ +'use strict'; // this line is required because ES6 classes are written in strict mode. + +// those of you who have background in OOD languages will probably wonder if JavaScript allows for private class variables. +// JavaScript is OOD-ish. +// Private variable naming convention is to prefix private variable names with underscore character. + +// >>>>> PRIVATE VARIABLE SET UP .... ugly, right? +let _items = new WeakMap(); +let _min = new WeakMap(); +let _count = new WeakMap(); +// <<<<< +class Queue { + // Your code here + constructor() { + // >>> instantiate private variable + _items.set(this, []); + _min.set(this, []); + _count.set(this, 0); + // <<< + } + + getLength() { + // >>> private variable access + let count = _count.get(this); + // <<< + return count; + } + + enqueue(item) { + // >>> private variable access + let items = _items.get(this); + let count = _count.get(this); + let min = _min.get(this); + // <<< + + items.unshift(item); + count = count + 1; + if(min.length === 0 || min[0] >= item){ + min.unshift(item); + } + + return items[0]; + } + + dequeue() { + // >>> private variable access + let items = _items.get(this); + let count = _count.get(this); + let min = _min.get(this); + // <<< + + if(count > 0) { + count = count - 1; + } + var item = items.shift(); + if(item === min[0]){ + min.shift(); + } + return item; + } + + peek() { + // >>> private variable access + let items = _items.get(this); + // <<< + return items.slice(0,1)[0]; + } + + getMin() { + // >>> private variable access + let min = _min.get(this); + // <<< + return min[0]; + } +}; + + +let q = new Queue(); + +console.log('en: ', q.enqueue(13)); +console.log(q.enqueue(12)); +console.log(q.enqueue(10)); +console.log(q.enqueue(1)); +console.log('de: ', q.dequeue()); +console.log(q.dequeue()); +console.log(q.dequeue()); +console.log(q.dequeue()); + + From 965daef7a45060ef1d8afdabfabe0c8e4c9880ce Mon Sep 17 00:00:00 2001 From: ClarabelleCheng-Yue Date: Thu, 17 Nov 2016 02:04:04 -0800 Subject: [PATCH 6/8] add queue implementation with linked list --- src/queue_LL.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/queue_LL.js diff --git a/src/queue_LL.js b/src/queue_LL.js new file mode 100644 index 0000000..a509c8f --- /dev/null +++ b/src/queue_LL.js @@ -0,0 +1,65 @@ +// IMPLEMENTATION USING LINKED LIST +var Queue = function() { + // private variables + var queueInstance = {}; + var head = null; + var tail = null; + + // private class + var Node = function(val) { + var nodeInstance = {}; + nodeInstance.value = val; + nodeInstance.next = null; + + return nodeInstance; + } + + queueInstance.enqueue = function(v) { + var newNode = Node(v); + + if(!head) { + // head = tail = newNode; <<<< shortcut + head = newNode; + tail = newNode; + } else { + tail.next = newNode; + tail = newNode; + } + ++this.length; + + return tail.value; + } + + queueInstance.dequeue = function() { + if (!head) { + console.error('Cannot dequeue empty queue'); + } else { + var n = head.value; + head = head.next; + --this.length; + return n; + } + + return undefined; + } + + queueInstance.length = 0; + + return queueInstance; +} + +var q = Queue(); +console.log('length at start: ', q.length); // 0 +console.log('enqueue: '); +for (var i = 0; i < 3; i++) { + console.log(q.enqueue(i)); // Head[0]Tail --> H[0, 1]T --> H[0, 1, 2]T +} + +console.log('length after adding 3 items: ', q.length); // 3 +console.log('dequeue: '); +for (var i = 0; i < 3; i++) { + console.log(q.dequeue()); // Head[0, 1, 2]Tail --> H[1, 2]T --> H[2]T +} + +console.log('length after removing everything: ', q.length); // 0 +q.dequeue(); // should log error message \ No newline at end of file From c955ba7c5ebbafc4d85a6d050b237af1e3d5b07d Mon Sep 17 00:00:00 2001 From: Clarabelle Cheng-Yue Date: Thu, 17 Nov 2016 11:31:56 -0800 Subject: [PATCH 7/8] Update queue_LL.js --- src/queue_LL.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/queue_LL.js b/src/queue_LL.js index a509c8f..43ae32c 100644 --- a/src/queue_LL.js +++ b/src/queue_LL.js @@ -52,14 +52,14 @@ var q = Queue(); console.log('length at start: ', q.length); // 0 console.log('enqueue: '); for (var i = 0; i < 3; i++) { - console.log(q.enqueue(i)); // Head[0]Tail --> H[0, 1]T --> H[0, 1, 2]T + console.log(q.enqueue(i)); // H[]T --> Head[0]Tail --> H[0, 1]T --> H[0, 1, 2]T } console.log('length after adding 3 items: ', q.length); // 3 console.log('dequeue: '); for (var i = 0; i < 3; i++) { - console.log(q.dequeue()); // Head[0, 1, 2]Tail --> H[1, 2]T --> H[2]T + console.log(q.dequeue()); // Head[0, 1, 2]Tail --> H[1, 2]T --> H[2]T --> H[]T } console.log('length after removing everything: ', q.length); // 0 -q.dequeue(); // should log error message \ No newline at end of file +q.dequeue(); // should log error message From 277e293ff34712fea916e78493789aa574040436 Mon Sep 17 00:00:00 2001 From: Clarabelle Cheng-Yue Date: Thu, 17 Nov 2016 23:29:02 -0800 Subject: [PATCH 8/8] Create stack_LL.js --- src/stack_LL.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/stack_LL.js diff --git a/src/stack_LL.js b/src/stack_LL.js new file mode 100644 index 0000000..8a9dffb --- /dev/null +++ b/src/stack_LL.js @@ -0,0 +1,37 @@ +// IMPLEMENTATION OF STACK using LINKED LIST +var Stack = function() { + var instance = {}; + + var Node = function(val) { + var node = {}; + node.value = val; + node.next = null; + return node; + } + var head = null; + + instance.length = 0; + + instance.pop = function() { + if (!head) { + console.error('Cannot pop empty stack'); + } else { + var result = head.value; + head = head.next; + --this.length; + } + + return result; + } + + instance.push = function(val) { + var newNode = Node(val); + newNode.next = head; + head = newNode; + ++this.length; + + return head.value; + } + + return instance; +};