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

Cedar-Bailey #7

Open
wants to merge 11 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
80 changes: 80 additions & 0 deletions lib/problems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const Stack = require("../lib/stack");

/*
Time Complexity: O(n) single loop
Space Complexity: O(n) single stack used
*/
const balanced = (string) => {

Choose a reason for hiding this comment

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

// if string is empty return true
if (string.length === 0) return true;

//initialize stack
let stack = new Stack;
// create hasp map for brackets/parentheses
let hash = {
'(' : ')',
'[' : ']',
'{' : '}'
};

// scan each element of the string
for (let i = 0; i < string.length; i++) {
// if the element is an opener, push to the stack
if (string[i] === '(' || string[i] === '{' || string[i] === '[') {
stack.push(string[i]);
} else if (hash[stack.pop()] !== string[i]) {
// else pop the stack, if that element is not the corresponding opener return false
// otherwise continue the loop
return false;
}
};

// if the stack is empty, return true
return stack.isEmpty() ? true : false;

}

/*
Time Complexity: O(n) single loop
Space Complexity: O(n) single stack
*/
const evaluatePostfix = (expr) => {

Choose a reason for hiding this comment

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

let stack = new Stack;

// scan each character in the expression
for(let i=0;i<expr.length;i++) {
let ch = expr[i];

if (ch >= '0' && ch <= '9') {
//convert string to integer
stack.push(parseInt(ch, 10));
} else {
let val1 = stack.pop();
let val2 = stack.pop();

// switch case that runs through the possible operators
switch (ch)
{
case '+':
stack.push(val2 + val1);
break;

case '-':
stack.push(val2 - val1);
break;

case '/':
stack.push(parseInt(val2 / val1, 10));
break;

case '*':
stack.push(val2 * val1);
break;
}
}
}
return stack.pop();
}

exports.balanced = balanced;
exports.evaluatePostfix = evaluatePostfix;
35 changes: 27 additions & 8 deletions lib/queue.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
class Queue {
constructor() {
// this.store = ...
throw new Error("This method has not been implemented!");
this.store = new Array(this.capacity);
this.length = 0;
this.capacity = 20;
this.head = 0;
this.tail = -1;
}

enqueue(element) {
throw new Error("This method has not been implemented!");
if (this.length >= this.capacity) return;

Choose a reason for hiding this comment

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


this.tail = (this.tail + 1) % this.capacity;
this.store[this.tail % this.capacity] = element;

Choose a reason for hiding this comment

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

Suggested change
this.store[this.tail % this.capacity] = element;
this.store[this.tail] = element;

this.length++;
}

dequeue() {
throw new Error("This method has not been implemented!");
if (this.isEmpty()) return;

Choose a reason for hiding this comment

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


let result = this.store[this.head % this.capacity];

this.store[this.head % this.capacity] = null;

this.head = (this.head + 1) % this.capacity;
this.length--;
return result;
}

front() {
throw new Error("This method has not been implemented!");
return this.head;

Choose a reason for hiding this comment

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

Here we want to return the value stored at this.head not the value stored in the pointer itself

}

size() {
throw new Error("This method has not been implemented!");
return this.length;

Choose a reason for hiding this comment

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

}

isEmpty() {
throw new Error("This method has not been implemented!");
return this.length === 0;

Choose a reason for hiding this comment

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

}

isFull() {
return this.length === this.capacity;

Choose a reason for hiding this comment

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

}

toString() {

Choose a reason for hiding this comment

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

let arr;
if (this.head > this.tail) {
arr = this.store.slice(this.head, this.capacity).concat(this.store.slice(0, this.tail));
arr = this.store.slice(this.head, this.capacity).concat(this.store.slice(0, this.tail + 1));
} else {
arr = this.store
}
Expand Down
17 changes: 10 additions & 7 deletions lib/stack.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
const LinkedList = require("./linked-list.js");

class Stack {
constructor() {
// this.store = ...
throw new Error("This method has not been implemented!");
this.store = new LinkedList();

Choose a reason for hiding this comment

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

}

push() {
throw new Error("This method has not been implemented!");
push(value) {

Choose a reason for hiding this comment

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

this.store.addLast(value);
}

pop() {
throw new Error("This method has not been implemented!");

Choose a reason for hiding this comment

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

let last = this.store.getLast();
this.store.delete(last);
return last;
}

isEmpty() {
throw new Error("This method has not been implemented!");
return this.store.isEmpty();

Choose a reason for hiding this comment

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

}

toString() {
JSON.stringify(this.store);
this.store.toString();

Choose a reason for hiding this comment

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

}
}

Expand Down
Loading