diff --git a/lib/problems.js b/lib/problems.js index 0c3ab21..275311f 100644 --- a/lib/problems.js +++ b/lib/problems.js @@ -1,18 +1,98 @@ +const Stack = require("./stack"); +const Queue = require("./queue"); + /* -Time Complexity: ? -Space Complexity: ? +Time Complexity: O(n) +Space Complexity: O(n) */ const balanced = (str) => { - throw new Error("This method has not been implemented!"); + const stack = new Stack; + const OPENING_BRACES = ['[', '{', '(']; + const CLOSING_BRACES = [']', '}', ')']; + let prevChar = null; + + for(let i = 0; i < str.length; i++) { + const newChar = str.charAt(i); + prevChar = stack.peek(); + + stack.push(newChar); + + if (CLOSING_BRACES.includes(newChar)) { + switch(newChar){ + case CLOSING_BRACES[0]: + if (prevChar == OPENING_BRACES[0]) { + stack.pop(); + stack.pop(); + } + break; + case CLOSING_BRACES[1]: + if (prevChar == OPENING_BRACES[1]) { + stack.pop(); + stack.pop(); + } + break; + case CLOSING_BRACES[2]: + if (prevChar == OPENING_BRACES[2]) { + stack.pop(); + stack.pop(); + } + break; + } + } + } + + return stack.isEmpty(); } /* -Time Complexity: ? -Space Complexity: ? +Time Complexity: O(n) +Space Complexity: O(n) */ const evaluatePostfix = (expr) => { - throw new Error("This method has not been implemented!"); + const OPERATORS = ['+', '-', '*', '/']; + const operandsQueue = new Queue; + const operatorsQueue = new Queue; + let result = null; + let operand1 = null; + + for(let i = 0; i < expr.length; i++) { + const currentChar = expr.charAt(i); + if (OPERATORS.includes(currentChar)) { + operatorsQueue.enqueue(currentChar); + } else { + operandsQueue.enqueue(parseInt(currentChar)); + } + } + + const numOperators = operatorsQueue.length(); + + for(let i = 0; i < numOperators; i++) { + if (operand1 == null) { + operand1 = operandsQueue.dequeue(); + } else { + operand1 = result; + } + let operator = operatorsQueue.dequeue(); + let operand2 = operandsQueue.dequeue(); + result = evaluateOperands(operator, operand1, operand2); + } + + return result; } +function evaluateOperands(operator, operand1, operand2) { + switch (operator) { + case '+': + return operand1 + operand2; + case '-': + return operand1 - operand2; + case '*': + return operand1 * operand2; + case '/': + return parseInt(operand1 / operand2); + } +} + + exports.balanced = balanced; exports.evaluatePostfix = evaluatePostfix; diff --git a/lib/queue.js b/lib/queue.js index 7a653f8..e39ef60 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -1,27 +1,56 @@ +//Using a circular buffer with an internal array starting at 20 elements + class Queue { constructor() { - // this.store = ... - throw new Error("This method has not been implemented!"); + this.store = new Array(20); + this.front = 0; + this.rear = 0; } - enqueue(element) { - throw new Error("This method has not been implemented!"); + enqueue(value) { + if (this.rear == (this.size() - 1)) { + this.rear = 0; + } else { + this.rear++; + } + this.store[this.rear] = value; } dequeue() { - throw new Error("This method has not been implemented!"); + if (this.front == (this.size() - 1)) { + this.front = 0; + } else { + this.front++; + } + + const dequeued = this.store[this.front]; + this.store[this.front] = null; + + return dequeued; } front() { - throw new Error("This method has not been implemented!"); + return this.store[front]; } size() { - throw new Error("This method has not been implemented!"); + let count = 0; + while (!!this.store[count]) { + count++; + } + return count; + } + + length() { + return this.rear - this.front; } isEmpty() { - throw new Error("This method has not been implemented!"); + if (this.front == this.rear) { + return true; + } else { + return false; + } } toString() { diff --git a/lib/stack.js b/lib/stack.js index dff9f6c..699466c 100644 --- a/lib/stack.js +++ b/lib/stack.js @@ -1,19 +1,31 @@ +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(value) { + this.store.addLast(value); + return this.store.getLast(); } pop() { - throw new Error("This method has not been implemented!"); + const popped = this.store.getLast(); + this.store.delete(popped); + return popped; + } + + peek() { + return this.store.getLast(); } isEmpty() { - throw new Error("This method has not been implemented!"); + if (this.store.length() == 0) { + return true; + } else { + return false; + } } toString() { diff --git a/test/problems.test.js b/test/problems.test.js index a14ff20..749ee8c 100644 --- a/test/problems.test.js +++ b/test/problems.test.js @@ -43,4 +43,4 @@ describe("test wave 3 problems", () => { expect(evaluatePostfix('62/5+')).toEqual(8); }); }); -}); \ No newline at end of file +}); diff --git a/test/queue.test.js b/test/queue.test.js index e1bec8c..353f117 100644 --- a/test/queue.test.js +++ b/test/queue.test.js @@ -101,4 +101,4 @@ 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]'); }); -}); \ No newline at end of file +}); diff --git a/test/stack.test.js b/test/stack.test.js index 6d5dbf5..9ef2dba 100644 --- a/test/stack.test.js +++ b/test/stack.test.js @@ -43,4 +43,4 @@ describe('test stack implementation', () => { const removed = stack.pop(); expect(removed).toEqual(7); }); -}); \ No newline at end of file +});