Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
You hit most of the learning goals here. In particular you didn't use a circular buffer to implement a Queue. See my notes. Let me know if you want help with this.
|
|
||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| pairs = {")" => "(", "}" => "{", "]"=>"["} |
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.push(element) |
There was a problem hiding this comment.
This isn't using a circular buffer, it works, but not what I requested.
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| removed = @store[0] | ||
| @store = @store[1..-1] |
There was a problem hiding this comment.
Just a note, dequeue is an O(n) operation because of how you're using the array here.
CheezItMan
left a comment
There was a problem hiding this comment.
This is mostly good, a few bugs here and there. I've added some tests for next cohort to help catch them. Take a look at my comments and let me know if you have questions.
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @front == 0 && @rear == @BUFFER_SIZE - 1 |
There was a problem hiding this comment.
This could run into trouble if the Queue becomes full and the front is not at index 0.
Consider if you had a buffer size of 10,
You add 9 elements, remove one (so front is at index 1) and then try to add 7 more. What happens?
|
|
||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| removed_data = @store[@front] |
There was a problem hiding this comment.
Before dequeuing you should check to see if the queue is empty.
| if (@front == @rear) | ||
| @front = -1 | ||
| @rear = -1 | ||
| elsif @front === @BUFFER_SIZE - 1 |
There was a problem hiding this comment.
This kind of elsif statements work or you could do: @front = (@front + 1) % BUFFER_SIZE
| display_store = @store[@front...@rear+1] | ||
| else | ||
| first_half = @store[0...@rear] | ||
| second_half = @store[@front..@store.length+1] |
Stacks and Queues
Thanks for doing some brain yoga. You are now submitting this assignment!
Comprehension Questions
OPTIONAL JobSimulation