-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: master
Are you sure you want to change the base?
🌊 - Richelle #4
Changes from all commits
91a8c04
edc0278
5f01da9
3f6a7b3
1837a06
7813fc5
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 |
---|---|---|
@@ -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) => { | ||
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
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. 👍 |
||
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; | ||
|
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) { | ||
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. 👍 |
||
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() { | ||
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. 👍 |
||
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() { | ||
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. 👍 , 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() { | ||
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. 👍 |
||
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() { | ||
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. 👍 |
||
throw new Error("This method has not been implemented!"); | ||
return this.tail === this.head; | ||
// return !this.store[this.head]; | ||
} | ||
|
||
toString() { | ||
|
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.
👍 I love the use of the JS Object here as a hash.