-
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
C16 - Cedar - Afina Walton #1
base: master
Are you sure you want to change the base?
Changes from all commits
2153c18
0cc731c
c6d4d3d
cbcc48f
00fed8d
0d77dac
bdee8ce
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,55 @@ | ||
function balanced(string) { | ||
// implement stack to check balanced strings | ||
let matches = { | ||
'(': ')', | ||
'{': '}', | ||
'[': ']', | ||
} | ||
|
||
let stack = []; | ||
// if open, add to stack | ||
for (let char of string) { | ||
if (char in matches) { | ||
stack.push(char); | ||
continue; | ||
} else { | ||
let popped = stack.pop(); | ||
if (matches[popped] === char) continue; | ||
else return false; | ||
} | ||
} | ||
|
||
if (stack.length) return false; | ||
return true; | ||
} | ||
|
||
function evaluatePostfix(expr) { | ||
let operations = { | ||
'+': add = (a, b) => a + b, | ||
'*': mult = (a, b) => a * b, | ||
'-': subtract = (a, b) => a - b, | ||
'/': divide = (a, b) => a / b, | ||
} | ||
|
||
let stack = []; | ||
let newNum = 0; | ||
|
||
for (let char of expr) { | ||
if (char in operations) { | ||
if (stack.length > 1) { | ||
let popA = +stack.shift(); | ||
let popB = +stack.shift(); | ||
newNum = Math.floor(operations[char](popA, popB)); | ||
} else if (stack.length === 1) { | ||
let popped = +stack.shift(); | ||
newNum = Math.floor(operations[char](newNum, popped)); | ||
} | ||
} else { | ||
stack.push(char); | ||
continue; | ||
} | ||
} | ||
if (stack.length === 0) return newNum; | ||
} | ||
|
||
module.exports = { balanced, evaluatePostfix } |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,27 +1,59 @@ | ||||||||
class Queue { | ||||||||
#front; | ||||||||
#rear; | ||||||||
|
||||||||
constructor() { | ||||||||
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. 👀 While your implementation does work and utilize Javascript arrays effectively, the readme asks you to implement a queue using a circular buffer. |
||||||||
// this.store = ... | ||||||||
throw new Error("This method has not been implemented!"); | ||||||||
this.store = new Array(20); | ||||||||
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. 👀 When implementing a circular buffer, it's often helpful to have a
Suggested change
|
||||||||
// front and rear are index refs | ||||||||
this.#front = this.#rear = 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. ✨ Curious as to why you chose to set the |
||||||||
} | ||||||||
|
||||||||
enqueue(element) { | ||||||||
throw new Error("This method has not been implemented!"); | ||||||||
enqueue(element) { | ||||||||
if ( | ||||||||
(this.#front === 0 && this.#rear === this.size() - 1) | ||||||||
) { | ||||||||
throw 'QueueFullException' | ||||||||
} | ||||||||
else if (this.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. ✨ Nice use of your other methods! |
||||||||
this.store[this.#front] = element; | ||||||||
} else { | ||||||||
this.#rear += 1; | ||||||||
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. 👀 Make sure you're handling the case where
Suggested change
|
||||||||
this.store[this.#rear] = element; | ||||||||
} | ||||||||
|
||||||||
return; | ||||||||
} | ||||||||
|
||||||||
dequeue() { | ||||||||
throw new Error("This method has not been implemented!"); | ||||||||
if (this.isEmpty()) { | ||||||||
throw 'QueueEmptyException' | ||||||||
} | ||||||||
|
||||||||
const data = this.store[this.#front]; | ||||||||
this.store[this.#front] = null; | ||||||||
|
||||||||
if (this.#front === this.#rear) { | ||||||||
this.#front = this.#rear = -1; | ||||||||
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. In your constructor, you initialize
Suggested change
|
||||||||
} else if (this.#front === this.size() - 1) { | ||||||||
this.#front = 0; | ||||||||
} else { | ||||||||
this.#front += 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. ✨ |
||||||||
throw new Error("This method has not been implemented!"); | ||||||||
return this.store[this.#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. ✨ |
||||||||
throw new Error("This method has not been implemented!"); | ||||||||
return this.store.length; | ||||||||
} | ||||||||
|
||||||||
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!"); | ||||||||
if (this.#front === this.#rear && !this.store[this.#front]) return true | ||||||||
return false; | ||||||||
} | ||||||||
|
||||||||
toString() { | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
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. 👀 While your implementation does work and utilize Javascript arrays effectively, unfortunately the readme requested you implement stacks using the included LinkedList class. 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(element) { | ||
this.store.addLast(element); | ||
return; | ||
} | ||
|
||
pop() { | ||
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 toRemove = this.store.getLast(); | ||
this.store.delete(toRemove); | ||
|
||
return toRemove; | ||
} | ||
|
||
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.store.isEmpty(); | ||
} | ||
|
||
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. 👀 You might consider LinkedList's |
||
|
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.
💫🌸 Impressive! You didn't need to implement this, but good work. I apologize for the confusion!