Skip to content

Branches - Kelsey#25

Open
kelsk wants to merge 5 commits into
Ada-C12:masterfrom
kelsk:master
Open

Branches - Kelsey#25
kelsk wants to merge 5 commits into
Ada-C12:masterfrom
kelsk:master

Conversation

@kelsk
Copy link
Copy Markdown

@kelsk kelsk commented Mar 2, 2020

Stacks and Queues

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

Comprehension Questions

Question Answer
What is an ADT? An Abstract Data Type is any data type that can be implemented any way, as long as it can perform the operations in its type description.
Describe a Stack A stack is a last-in, first-out abstract data type.
What are the 5 methods in Stack and what does each do? A Stack can perform these operations - push: adds an element to the stack, pop: removes the last item from the stack, empty: checks whether the stack is empty, to_s: returns a string of the array, initialize: initializes.
Describe a Queue A queue is a first-in, first-out data type, which can be maintained with a circular buffer.
What are the 5 methods in Queue and what does each do? Enqueue: adds an item to the queue, dequeue: removes the first item from the queue, empty: checks whether the queue is empty, front: returns the value at the front of the queue, size: returns the length of the queue, to_s: returns a string of the array without the nils.
What is the difference between implementing something and using something? Implementation is initializing a class, setting up a data structure and methods, so that it is able to be used. Using something is calling the implementation and running the methods.

OPTIONAL JobSimulation

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

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.

Some issues with the Queue class. Take a look at my comments and let me know if you have any questions. The other parts seem to work well.

Comment thread lib/problems.rb
Comment on lines +3 to 5
# Time Complexity: O(n)
# Space Complexity: O(n)
def balanced(string)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

Comment thread lib/problems.rb
Comment on lines +38 to +42
if stack.empty?
return true
else
return false
end
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
if stack.empty?
return true
else
return false
end
return stack.empty?

Comment thread lib/queue.rb
# remove dequeued item from queue
@store[@front] = nil
# move front to next item in queue
@front = @front+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.

You need to make sure the front will wrap around the queue

Suggested change
@front = @front+1
@front = (@front+1) % @store.length

You also need to check to see if the queue is empty!

Comment thread lib/queue.rb
Comment on lines 38 to 40
def size
raise NotImplementedError, "Not yet implemented"
return @store.compact.length
end
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 works, but it's not very efficient.

Comment thread lib/queue.rb
end

def to_s
@store.compact!
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 compacts the buffer which removes any extra space you have in the buffer, which you might need in the Queue!

Better to do something like this:

    return "[]" if @front == -1
    current = @front
    output = "["
    next = (@front + 1) % @store.length
    while next != @back
      output += "#{@store[current]},"
      current = (current + 1) % @store.length
      next = (next + 1) % @store.length
    end

    output += "#{@store[current]}]"
    return output

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