-
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
Water - Christabel #2
base: master
Are you sure you want to change the base?
Changes from all commits
204f4e1
031f9ae
63f93bf
381774a
f5e6ae4
b16469a
bab67a3
59b521b
e81c116
8d273de
2827cd4
192245e
bc364ed
ddb0f8e
498a10c
0e4c224
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,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) => { | ||
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
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!"); | ||
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; |
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) { | ||||||||||||||
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.rear == (this.size() - 1)) { | ||||||||||||||
this.rear = 0; | ||||||||||||||
} else { | ||||||||||||||
this.rear++; | ||||||||||||||
} | ||||||||||||||
this.store[this.rear] = value; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
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.front == (this.size() - 1)) { | ||||||||||||||
this.front = 0; | ||||||||||||||
} else { | ||||||||||||||
this.front++; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
const dequeued = this.store[this.front]; | ||||||||||||||
this.store[this.front] = null; | ||||||||||||||
|
||||||||||||||
return dequeued; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
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. 👍 |
||||||||||||||
throw new Error("This method has not been implemented!"); | ||||||||||||||
return this.store[front]; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
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. 🤔 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
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. Maybe just do this
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
toString() { | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
const LinkedList = require("./linked-list"); | ||
|
||
class Stack { | ||
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. 👍 |
||
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() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,4 @@ describe("test wave 3 problems", () => { | |
expect(evaluatePostfix('62/5+')).toEqual(8); | ||
}); | ||
}); | ||
}); | ||
}); |
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.
👍