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

🌊 - Richelle #4

Open
wants to merge 6 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
66 changes: 60 additions & 6 deletions lib/problems.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,71 @@
const Stack = require('./stack')

/*
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n) worst case if all are opening brackets
Space Complexity: same as above
*/
const balanced = (str) => {
Comment on lines +4 to 7

Choose a reason for hiding this comment

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

👍 I love the use of the JS Object here as a hash.

throw new Error("This method has not been implemented!");
const openers = {
'{': '}',
'[': ']',
'(': ')'
}

let stack = new Stack;
for (const char of str) {
if (openers[char]) { // the char is an opening bracket
stack.push(char);
} else { // the char is a closing bracker
// pop off of the stack to see if it's the correct opener
if (openers[stack.pop()] !== char) return false;
}
}

return stack.isEmpty();
}

/*
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(n)
*/
const evaluatePostfix = (expr) => {
Comment on lines +28 to 31

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!");
// assumes only the operators listed below would be given
// also any non-operator would be an integer
const operators = {'+': add, '-': subtract, '/': divide, '*': multiply};
let stack = new Stack;

for (const char of expr) {
if (operators[char]) {
const secondNum = stack.pop();
const firstNum = stack.pop();

stack.push(operators[char](parseInt(firstNum), parseInt(secondNum)))
} else {
stack.push(char);
}
}

return parseInt(stack.pop());
}

const add = (num1, num2) => {
return (num1 + num2);
}

const subtract = (num1, num2) => {
return (num1 - num2);
}

const multiply = (num1, num2) => {
return (num1 * num2)
}

const divide = (num1, num2) => {
if (num2 === 0) {
throw new Error('Cannot divide by 0');
} else {
return (num1 / num2);
}
}

exports.balanced = balanced;
Expand Down
53 changes: 44 additions & 9 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,62 @@
class Queue {
constructor() {
// this.store = ...
throw new Error("This method has not been implemented!");
constructor(size = 20) {
this.store = new Array(size);
this.maxSize = size;
this.head = -1;
this.tail = -1;
}

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.tail + 1) % this.maxSize === this.head) || (this.head == 0 && this.tail == this.store.length -1)) {
throw new Error('Queue is full.');
} else if (this.head === -1) {
this.head = 0;
this.tail = 1;
this.store[this.head] = value;
} else {
this.store[this.tail] = value;
this.tail = (this.tail + 1) % this.maxSize;
}

}

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.head === -1) throw new Error("The queue is empty.");

const data = this.store[this.head];
this.store[this.head] = null;

// increment the head (consider wrapping around)
this.head = (this.head + 1) % this.maxSize;

// if head === tail then the list is empty
// reset head and tail to -1
if (this.head === this.tail) this.head = this.tail = -1;

return data;
}

front() {

Choose a reason for hiding this comment

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

👍 , you might also want to check to see if the queue is empty here.

throw new Error("This method has not been implemented!");
return this.store[this.head];
}

size() {

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!");
let count = 0;
let visited = 0;

while (visited < this.maxSize) {
if (!!this.store[visited]) {
count ++;
}
visited ++;
}

return count;
}

isEmpty() {

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.tail === this.head;
// return !this.store[this.head];
}

toString() {
Expand Down
15 changes: 9 additions & 6 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -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(item) {
this.store.addFirst(item);
}

pop() {
throw new Error("This method has not been implemented!");
const 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() {
Expand Down
Loading