From 541b42ade9d497dbcfc5280f39de6610e84c2b46 Mon Sep 17 00:00:00 2001 From: angethuy Date: Wed, 6 Jan 2021 15:17:42 -0800 Subject: [PATCH 1/3] feat: implement queue, stack, and balanced --- lib/problems.js | 24 +++++++++++++++++++++--- lib/queue.js | 24 ++++++++++++++++-------- lib/stack.js | 15 +++++++++------ 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/lib/problems.js b/lib/problems.js index 0c3ab21..bcbc3dc 100644 --- a/lib/problems.js +++ b/lib/problems.js @@ -1,9 +1,27 @@ +const Stack = require("./stack") + /* -Time Complexity: ? -Space Complexity: ? +Time Complexity: O(n) +Space Complexity: O(n) */ const balanced = (str) => { - throw new Error("This method has not been implemented!"); + var symbols = { + "{":"}", + "[":"]", + "(":")", + } + + const stack = new Stack(); + const closings = Object.values(symbols); + if (str.length % 2 !== 0) { return false } + for (let i = 0; i < str.length; i++) { + if (str[i] in symbols) { // push to stack since it's a key + stack.push(str[i]) + } else if (closings.includes(str[i])) { + if (symbols[stack.pop()] !== str[i]) { return false } + } + } + return stack.isEmpty() } /* diff --git a/lib/queue.js b/lib/queue.js index 7a653f8..02d3d7b 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -1,27 +1,35 @@ class Queue { - constructor() { - // this.store = ... - throw new Error("This method has not been implemented!"); + constructor(size = 20) { + this.store = new Array(size); + this.head = 0; + this.contains = 0; } enqueue(element) { - throw new Error("This method has not been implemented!"); + if (this.contains === 20) { throw new Error("The queue is full"); } + this.store[this.contains%this.store.length] = element; + this.contains++; } dequeue() { - throw new Error("This method has not been implemented!"); + if (this.contains === 0) { throw new Error("The queue is empty"); } + let element = this.store[this.head]; + this.store[this.head] = null; + this.head = (this.head + 1) % this.contains; + this.contains--; + return element; } front() { - throw new Error("This method has not been implemented!"); + return this.store[this.head] } size() { - throw new Error("This method has not been implemented!"); + return this.contains } isEmpty() { - throw new Error("This method has not been implemented!"); + return (this.contains === 0) } toString() { diff --git a/lib/stack.js b/lib/stack.js index dff9f6c..c134d70 100644 --- a/lib/stack.js +++ b/lib/stack.js @@ -1,19 +1,22 @@ +const LinkedList = require("./linked-list"); + class Stack { constructor() { - // this.store = ... - throw new Error("This method has not been implemented!"); + this.store = new LinkedList(); } - push() { - throw new Error("This method has not been implemented!"); + push(element) { + this.store.addFirst(element); } pop() { - throw new Error("This method has not been implemented!"); + let first = this.store.getFirst(); + this.store.delete(first); + return first; } isEmpty() { - throw new Error("This method has not been implemented!"); + return this.store.isEmpty(); } toString() { From 8a5cea1f2cbdb762b2b3af9a2c9bd9a4e6af4d9e Mon Sep 17 00:00:00 2001 From: angethuy Date: Wed, 6 Jan 2021 16:00:58 -0800 Subject: [PATCH 2/3] fix: update queue to correctly be circular --- lib/queue.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/queue.js b/lib/queue.js index 02d3d7b..4ad70d3 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -7,7 +7,7 @@ class Queue { enqueue(element) { if (this.contains === 20) { throw new Error("The queue is full"); } - this.store[this.contains%this.store.length] = element; + this.store[(this.head+this.contains)%this.store.length] = element; this.contains++; } @@ -15,7 +15,7 @@ class Queue { if (this.contains === 0) { throw new Error("The queue is empty"); } let element = this.store[this.head]; this.store[this.head] = null; - this.head = (this.head + 1) % this.contains; + this.head = ((this.head % this.store.length) + 1) % this.contains; this.contains--; return element; } From c4adec6457369f0eeb4e1d12f719a7fe68096f39 Mon Sep 17 00:00:00 2001 From: angethuy Date: Wed, 6 Jan 2021 16:01:19 -0800 Subject: [PATCH 3/3] test: add tests to check if circular queue --- test/queue.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/queue.test.js b/test/queue.test.js index 353f117..753ae83 100644 --- a/test/queue.test.js +++ b/test/queue.test.js @@ -101,4 +101,29 @@ describe("test queue implementation", () => { expect(q.toString()).toEqual('[40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210]'); }); + it('throws error for adding to full queue', () => { + const q = new Queue(); + for (let i = 0; i < 20; i++) { + q.enqueue(i); + } + expect(q.size()).toEqual(20) + expect(() => { + q.enqueue(100) + }).toThrow("The queue is full"); + }); + + it('is a circular queue', () => { + const q = new Queue(); + for (let i = 0; i < 20; i++) { + q.enqueue(i); + } + expect(q.size()).toEqual(20) + q.dequeue() + q.dequeue() + expect(q.size()).toEqual(18) + let i = q.dequeue() + expect(i).toEqual(2) + q.enqueue(100) + expect(q.store[0]).toEqual(100) + }); });