Skip to content
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

Niv #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Niv #19

wants to merge 4 commits into from

Conversation

nidhiparixitpatel
Copy link

Stacks and Queues

Thanks for doing some brain yoga. You are now submitting this assignment!

Comprehension Questions

Question Answer
What is an ADT? Abstract Data Type
Describe a Stack Stack is LIFO data structure that can use either an array or linked list
What are the 5 methods in Stack and what does each do? The 5 methods of a Stack are push (add element), pop (remove last element), empty? (return true if empty), to_s(list string of contents in Stack), initialize(creates new empty stack)
Describe a Queue Queue is FIFO data structure
What are the 5 methods in Queue and what does each do? The 5 methods of a queue are enqueue (add to beginning), dequeue (remove from begining), front (give value of first element), size (give size of queue), empty?(return true if empty)
What is the difference between implementing something and using something?

OPTIONAL JobSimulation

Question Answer
Did you include a sample run of your code as a comment?

@nidhiparixitpatel nidhiparixitpatel changed the title Niv - Sockets Niv Sep 10, 2019
Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@@ -1,7 +1,26 @@
require_relative './stack.rb'

def balanced(string)
raise NotImplementedError, "Not implemented yet"
pairs = {")" => "(", "}" => "{", "]"=>"["}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of a hash!

lib/queue.rb Outdated
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
@store.push(element)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't using a circular buffer, it works, but not what I requested.

lib/queue.rb Outdated
end

def dequeue
raise NotImplementedError, "Not yet implemented"
removed = @store[0]
@store = @store[1..-1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, dequeue is an O(n) operation because of how you're using the array here.

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if @front == 0 && @rear == @BUFFER_SIZE - 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

end

def dequeue
raise NotImplementedError, "Not yet implemented"
removed_data = @store[@front]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before dequeuing you should check to see if the queue is empty.

if (@front == @rear)
@front = -1
@rear = -1
elsif @front === @BUFFER_SIZE - 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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[@[email protected]+1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this actually works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants