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

Leaves-Yasmin #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions .github/PULL_REQUEST_TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@ Thanks for doing some brain yoga. You are now submitting this assignment!
## Comprehension Questions
| Question | Answer |
|------------ | --------- |
| What is an ADT? | |
| Describe a Stack | |
| What are the 5 methods in Stack and what does each do? | |
| Describe a Queue | |
| What are the 5 methods in Queue and what does each do? | |
| What is the difference between implementing something and using something? | |
| What is an ADT? | --> Abstract Data type (ADT) is a type (or class) for objects
whose behaviour is defined by a set of value and a set of operations. |

| Describe a Stack | --> A stack is an abstract data type, (Last in First out) that serves as a collection of elements,
with two principal operations: push, which adds an element to the collection, and
pop, which removes the most recently added element that was not yet removed. |

| What are the 5 methods in Stack and what does each do? | push : Pushes an element on the top of the stack.
pop : Removes and returns the top element of the stack.
empty : It returns true if nothing is on the top of the stack ( if the stack is empty). Else, returns false.
peek : Returns the element on the top of the stack, but does not remove it. |

| Describe a Queue | A Queue is an Abstract Data Structure which stores data and allows access in a First-In-First-Out order (FIFO)/last in last out. |

| What are the 5 methods in Queue and what does each do? | enqueue: add an element to the rear of the queue
dequeue: Remove & return the element from the front
is_empty: returns true if the queue is empty |

| What is the difference between implementing something and using something? | Implementing something: is to build it and know the how it works. Using something: using existing code |

## OPTIONAL JobSimulation
| Question | Answer |
Expand Down
49 changes: 42 additions & 7 deletions lib/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,65 @@ class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
@store = Array.new(100)
@front = @back = -1
# @front = -1
# @back = -1
end

def enqueue(element)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
if @front == -1 && @back == -1
@front = 0
@back = 1
@store[@front] = element
elsif @front == @back
#DECIDE
raise ArgumentError, "Queue is full!"
else
@store[@back] = element
@back = (@back + 1) % @store.length
end
end

def dequeue
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
if @front == @back
raise ArgumentError, "Queue is empty!"
end
if element = @store[@front]
@front = (@front + 1) % @store.length
return element
Comment on lines +32 to +34

Choose a reason for hiding this comment

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

The problem here is determining if the queue is empty or full because in either case front would be equal to back.

So

Suggested change
if element = @store[@front]
@front = (@front + 1) % @store.length
return element
if element = @store[@front]
@front = (@front + 1) % @store.length
@front = @back = -1 if @front == @back
return element

end
end

def front
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
return @store[@front]
end

def size
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
return @store.length

Choose a reason for hiding this comment

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

This only gives you the length of the internal storage, not the size of the queue.

end

def empty?
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
if @front == @back
return true
else
return false
end
Comment on lines +50 to +54

Choose a reason for hiding this comment

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

Suggested change
if @front == @back
return true
else
return false
end
return @front == @back

end

def to_s

Choose a reason for hiding this comment

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

👍

return @store.to_s
# return @store.to_s
queue = []
until @front == @back
queue << @store[@front]
@front = (@front + 1) % @store.length
end
return queue.to_s
end
end
15 changes: 11 additions & 4 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
require_relative "./linked_list"

class Stack
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
@store = LinkedList.new
end

def push(element)
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
@store.add_last(element)
end

def pop
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
return nil if self.empty?
@store.remove_last
end

def empty?
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
return @store.empty?
end

def to_s
Expand Down
8 changes: 0 additions & 8 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
end

it "adds something to an empty Queue" do
skip
q = Queue.new
q.enqueue(10)
expect(q.to_s).must_equal "[10]"
end

it "adds multiple somethings to a Queue" do
skip
q = Queue.new
q.enqueue(10)
q.enqueue(20)
Expand All @@ -27,13 +25,11 @@
end

it "starts the size of a Queue at 0" do
skip
q = Queue.new
q.empty?.must_equal true
end

it "a Queue is empty after removing all the elements" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(6)
Expand All @@ -43,7 +39,6 @@
end

it "removes something from the Queue" do
skip
q = Queue.new
q.enqueue(5)
removed = q.dequeue
Expand All @@ -52,7 +47,6 @@
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -63,7 +57,6 @@
end

it "properly adjusts the size with enqueueing and dequeueing" do
skip
q = Queue.new
q.empty?.must_equal true
q.enqueue(-1)
Expand All @@ -75,7 +68,6 @@
end

it "returns the front element in the Queue" do
skip
q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand Down
5 changes: 0 additions & 5 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
end

it "pushes something onto a empty Stack" do
skip
s = Stack.new
s.push(10)
s.to_s.must_equal "[10]"
end

it "pushes multiple somethings onto a Stack" do
skip
s = Stack.new
s.push(10)
s.push(20)
Expand All @@ -26,13 +24,11 @@
end

it "starts the stack empty" do
skip
s = Stack.new
s.empty?.must_equal true
end

it "removes something from the stack" do
skip
s = Stack.new
s.push(5)
removed = s.pop
Expand All @@ -41,7 +37,6 @@
end

it "removes the right something (LIFO)" do
skip
s = Stack.new
s.push(5)
s.push(3)
Expand Down