diff --git a/lib/problems.js b/lib/problems.js index 1b2f25a..dfe1253 100644 --- a/lib/problems.js +++ b/lib/problems.js @@ -1,9 +1,38 @@ /* -Time Complexity: ? -Space Complexity: ? +Time Complexity: O(n) +Space Complexity: O(n) */ + +const Stack = require("./stack"); + const balanced = (str) => { - throw new Error("This method has not been implemented!"); + if (str === "") { + return true; + } + + if (str.length % 2 !== 0) { + return false + } + + const stack = new Stack(); + + const parens = { + "{": "}", + "(": ")", + "[": "]", + } + + for (let i = 0; i < str.length; i++) { + if(str[i] === "{" || str[i] === "(" || str[i] === "[") { + stack.push(str[i]) + } else if(stack.isEmpty() || (parens[stack.peek()] !== str[i])) { + return false + } else { + stack.pop(); + } + } + + return stack.isEmpty(); } /* @@ -14,4 +43,4 @@ const evaluatePostfix = (expr) => { throw new Error("This method has not been implemented!"); } -exports = { balanced, evaluatePostfix }; \ No newline at end of file +module.exports = { balanced, evaluatePostfix }; \ No newline at end of file diff --git a/lib/queue.js b/lib/queue.js index 7a653f8..360fbe7 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -1,35 +1,60 @@ class Queue { - constructor() { - // this.store = ... - throw new Error("This method has not been implemented!"); + constructor(capacity = 20) { + this.capacity = capacity; + this.array = []; + this.front = -1; + this.rear = -1; } enqueue(element) { - throw new Error("This method has not been implemented!"); + if (this.front === -1) { + this.front = 0; + this.rear = 1; + this.array[this.front] = element; + } else if (this.front === this.rear) { + throw new Error("The queue is full"); + } else { + this.array[this.rear] = element; + this.rear = (this.rear + 1) % this.capacity; + } } dequeue() { - throw new Error("This method has not been implemented!"); + if (this.front === -1) { + throw new Error("The queue is empty"); + } + + let data = this.array[this.front]; + this.array[this.front] = null; + + this.front = (this.front + 1) % this.capacity; + if (this.front === this.rear) { + this.front = -1; + this.rear = -1; + } + + return data; } front() { - throw new Error("This method has not been implemented!"); + return this.array[front]; } size() { - throw new Error("This method has not been implemented!"); + return this.array.length; } isEmpty() { - throw new Error("This method has not been implemented!"); + return this.front === -1; } toString() { + let arr; - if (this.head > this.tail) { - arr = this.store.slice(this.head, this.capacity).concat(this.store.slice(0, this.tail)); + if (this.front > this.rear) { + arr = this.array.slice(this.front, this.capacity).concat(this.array.slice(0, this.rear)); } else { - arr = this.store + arr = this.array } return JSON.stringify(arr.filter((v) => v !== null)); } diff --git a/lib/stack.js b/lib/stack.js index dff9f6c..ce938fe 100644 --- a/lib/stack.js +++ b/lib/stack.js @@ -1,24 +1,32 @@ +const LinkedList = require("./linked-list"); + class Stack { constructor() { - // this.store = ... - throw new Error("This method has not been implemented!"); + this.list = new LinkedList(); } - push() { - throw new Error("This method has not been implemented!"); + push(item) { + this.list.addFirst(item); } pop() { - throw new Error("This method has not been implemented!"); + let first = this.list.getFirst(); + this.list.delete(first); + + return first; } isEmpty() { - throw new Error("This method has not been implemented!"); + return this.list.isEmpty(); } toString() { JSON.stringify(this.store); } + + peek() { + return this.list.getFirst(); + } } module.exports = Stack; \ No newline at end of file diff --git a/test/problems.test.js b/test/problems.test.js index 56a0a64..919c0b0 100644 --- a/test/problems.test.js +++ b/test/problems.test.js @@ -1,6 +1,6 @@ const { balanced, evaluatePostfix } = require('../lib/problems'); -describe.skip("test wave 3 problems", () => { +describe("test wave 3 problems", () => { describe("balanced", () => { it('should return true given balanced strings', () => { expect(balanced('(({}))')).toEqual(true); @@ -26,7 +26,7 @@ describe.skip("test wave 3 problems", () => { }); }); - describe("postfix", () => { + describe.skip("postfix", () => { it('can add 2 numbers together', () => { expect(evaluatePostfix('34+')).toEqual(7); expect(evaluatePostfix('34*')).toEqual(12); diff --git a/test/queue.test.js b/test/queue.test.js index f9afe2e..e1bec8c 100644 --- a/test/queue.test.js +++ b/test/queue.test.js @@ -1,6 +1,6 @@ const Queue = require("../lib/queue"); -describe.skip("test queue implementation", () => { +describe("test queue implementation", () => { it("creates a queue", () => { const q = new Queue(); expect(q).toBeInstanceOf(Queue); diff --git a/test/stack.test.js b/test/stack.test.js index ab0f9e8..6d5dbf5 100644 --- a/test/stack.test.js +++ b/test/stack.test.js @@ -1,6 +1,6 @@ const Stack = require("../lib/stack"); -describe.skip('test stack implementation', () => { +describe('test stack implementation', () => { it("creates a queue", () => { const stack = new Stack(); expect(stack).toBeInstanceOf(Stack);