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

Branches - Kelsey #25

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

Branches - Kelsey #25

wants to merge 5 commits into from

Conversation

kelsk
Copy link

@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

@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 on lines +3 to 5
# Time Complexity: O(n)
# Space Complexity: O(n)
def balanced(string)

Choose a reason for hiding this comment

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

👍

Comment on lines +38 to +42
if stack.empty?
return true
else
return false
end

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?

# remove dequeued item from queue
@store[@front] = nil
# move front to next item in queue
@front = @front+1

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 on lines 38 to 40
def size
raise NotImplementedError, "Not yet implemented"
return @store.compact.length
end

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.

def to_s
@store.compact!

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