-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: master
Are you sure you want to change the base?
Cedar-Bailey #7
Changes from all commits
38754b0
57094af
ae2a7a2
c846001
d876cf9
8a9d81a
2724a8f
e4cdd3e
3b9430c
a9c3a35
2d2a9c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) => { | ||
// 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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
this.length++; | ||||||
} | ||||||
|
||||||
dequeue() { | ||||||
throw new Error("This method has not been implemented!"); | ||||||
if (this.isEmpty()) return; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we want to return the value stored at |
||||||
} | ||||||
|
||||||
size() { | ||||||
throw new Error("This method has not been implemented!"); | ||||||
return this.length; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||||||
} | ||||||
|
||||||
isFull() { | ||||||
return this.length === this.capacity; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||||||
} | ||||||
|
||||||
toString() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
} | ||||||
|
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
} | ||
|
||
push() { | ||
throw new Error("This method has not been implemented!"); | ||
push(value) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
} | ||
|
||
toString() { | ||
JSON.stringify(this.store); | ||
this.store.toString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨