Branches - Erika#33
Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Some issues with Queue and your balanced method here. Take a look at my comments and let me know what questions you have.
| openers = ["{", "[", "("] | ||
| closers = ["}", "]", ")"] | ||
|
|
There was a problem hiding this comment.
I suggest using a hash here instead of arrays where the key is an open brace and the value is the close.
| until count == length || stack_size > string.length/2 | ||
| char = string[count] | ||
| # if the value is in openers, add to stack | ||
| if openers.include?(char) | ||
| stack.push(char) | ||
| stack_size += 1 | ||
| # if the value is in closers, pop from stack | ||
| else | ||
| if closers.include?(char) | ||
| stack.pop | ||
| stack_size -= 1 | ||
| # if it's in neither, return false | ||
| else | ||
| return false | ||
| end | ||
| end | ||
| count += 1 | ||
| end |
There was a problem hiding this comment.
You're on the right track here. I suggest, like I did above using a hash to match up open and close braces.
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| end | ||
| def enqueue(element) |
There was a problem hiding this comment.
What about when the queue is empty, and what about when it's full?
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return true if @front == -1 && @rear == -1 |
There was a problem hiding this comment.
You're assuming the queue is empty if front and rear are -1, but your initialize method sets them to 0.
Also when you dequeue above if the queue is now empty, you would then need to set them to -1.
…_first as push/pop for best time/space
CheezItMan
left a comment
There was a problem hiding this comment.
Nice work Erika. You hit the main learning goals here, well done.
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def balanced(string) |
|
|
||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| def dequeue |
| return @store[@front] | ||
| end | ||
|
|
||
| def size |
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @front == @rear | ||
| end |
There was a problem hiding this comment.
I think this should actually be:
return @front == -1 && @rear == -1
Stacks and Queues
Thanks for doing some brain yoga. You are now submitting this assignment!
Comprehension Questions
OPTIONAL JobSimulation