Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions lib/problems.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
/*
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(n)
*/

const Stack = require("./stack");
Comment on lines +2 to +6

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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();
}

/*
Expand All @@ -14,4 +43,4 @@ const evaluatePostfix = (expr) => {
throw new Error("This method has not been implemented!");
}

exports = { balanced, evaluatePostfix };
module.exports = { balanced, evaluatePostfix };
47 changes: 36 additions & 11 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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;
}
Comment on lines 43 to 45

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really should be the number of elements used in the Queue.


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));
}
Expand Down
20 changes: 14 additions & 6 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
const LinkedList = require("./linked-list");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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;
4 changes: 2 additions & 2 deletions test/problems.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/queue.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/stack.test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down