Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Water - Christabel #2

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
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
92 changes: 86 additions & 6 deletions lib/problems.js
Original file line number Diff line number Diff line change
@@ -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) => {
Comment on lines +5 to 8

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!");
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) => {
Comment on lines +48 to 51

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!");
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;
45 changes: 37 additions & 8 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -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) {

Choose a reason for hiding this comment

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

👍

if (this.rear == (this.size() - 1)) {
this.rear = 0;
} else {
this.rear++;
}
this.store[this.rear] = value;
}

dequeue() {

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 == (this.size() - 1)) {
this.front = 0;
} else {
this.front++;
}

const dequeued = this.store[this.front];
this.store[this.front] = null;

return dequeued;
}

front() {

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!");
return this.store[front];
}

size() {

Choose a reason for hiding this comment

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

🤔 I'm not sure this this will work in all cases.

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;
}
Comment on lines +49 to +53

Choose a reason for hiding this comment

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

Maybe just do this

Suggested change
if (this.front == this.rear) {
return true;
} else {
return false;
}
return this.front == this.rear;

}

toString() {
Expand Down
24 changes: 18 additions & 6 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
const LinkedList = require("./linked-list");

class Stack {

Choose a reason for hiding this comment

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

👍

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() {
Expand Down
2 changes: 1 addition & 1 deletion test/problems.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ describe("test wave 3 problems", () => {
expect(evaluatePostfix('62/5+')).toEqual(8);
});
});
});
});
2 changes: 1 addition & 1 deletion test/queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]');
});

});
});
2 changes: 1 addition & 1 deletion test/stack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ describe('test stack implementation', () => {
const removed = stack.pop();
expect(removed).toEqual(7);
});
});
});