Skip to content

Niv#19

Open
nidhiparixitpatel wants to merge 4 commits intoAda-C11:masterfrom
nidhiparixitpatel:master
Open

Niv#19
nidhiparixitpatel wants to merge 4 commits intoAda-C11:masterfrom
nidhiparixitpatel:master

Conversation

@nidhiparixitpatel
Copy link
Copy Markdown

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
Copy Markdown

@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.

Comment thread lib/problems.rb

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

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!

Comment thread lib/queue.rb Outdated

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

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.

Comment thread lib/queue.rb Outdated
def dequeue
raise NotImplementedError, "Not yet implemented"
removed = @store[0]
@store = @store[1..-1]
Copy link
Copy Markdown

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
Copy Markdown

@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.

Comment thread lib/queue.rb

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

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?

Comment thread lib/queue.rb

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

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.

Comment thread lib/queue.rb
if (@front == @rear)
@front = -1
@rear = -1
elsif @front === @BUFFER_SIZE - 1
Copy link
Copy Markdown

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

Comment thread lib/queue.rb
display_store = @store[@front...@rear+1]
else
first_half = @store[0...@rear]
second_half = @store[@front..@store.length+1]
Copy link
Copy Markdown

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