-
Notifications
You must be signed in to change notification settings - Fork 41
India - Water #26
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
base: master
Are you sure you want to change the base?
India - Water #26
Conversation
CheezItMan
left a comment
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 had a few comments on the circular buffer-Queue, and let me know what questions you have. Otherwise this is well done. Nice work.
| if @front == 0 && @back == MAX_SIZE - 1 || @back == (@front - 1) % (MAX_SIZE - 1) | ||
| raise ArgumentError | ||
| elsif @front == -1 | ||
| @front = 0 | ||
| @back = 0 | ||
| elsif @back == MAX_SIZE - 1 && @front != 0 | ||
| @back = 0 | ||
| else | ||
| @back += 1 | ||
| end | ||
| @store[@back] = element |
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.
| if @front == 0 && @back == MAX_SIZE - 1 || @back == (@front - 1) % (MAX_SIZE - 1) | |
| raise ArgumentError | |
| elsif @front == -1 | |
| @front = 0 | |
| @back = 0 | |
| elsif @back == MAX_SIZE - 1 && @front != 0 | |
| @back = 0 | |
| else | |
| @back += 1 | |
| end | |
| @store[@back] = element | |
| if @front == (@back + 1) % MAX_SIZE | |
| raise ArgumentError, "Queue is full" | |
| elsif @front == -1 | |
| @front = 0 | |
| end | |
| @back = (@back + 1) % MAX_SIZE | |
| @store[@back] = element |
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @front == 0 && @back == MAX_SIZE - 1 || @back == (@front - 1) % (MAX_SIZE - 1) |
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.
This mostly works but look at the 2nd OR condition here.
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return nil if @store.empty? | ||
| raise ArgumentError if @front == -1 |
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.
@front is only going to be -1 if the queue is empty.
| return @store[@front] | ||
| end | ||
|
|
||
| def size |
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.
👍
| return @front < @back ? @back - @front + 1 : MAX_SIZE - @front + @back | ||
| end | ||
|
|
||
| def empty? |
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.
👍
| def to_s | ||
| return @store.to_s | ||
| result_array = [] | ||
| 20.times do |i| |
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.
Do you need the magic number 20.times loop? Could you instead start i at @front and keep incrementing until you hit the back?
| @@ -1,19 +1,21 @@ | |||
| class Stack | |||
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.
👍
Stacks and Queues
Thanks for doing some brain yoga. You are now submitting this assignment!
Comprehension Questions
OPTIONAL JobSimulation