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 - Samantha #30

Open
wants to merge 4 commits 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
64 changes: 47 additions & 17 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@
class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(30)
@front = @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"
# queue is empty
if @front == -1 && @back == -1
@front = 0
@back = 1
@store[@front] = element

elsif @front == @back
#queue is full
raise ArgumentError, "Queue is full"

else
@store[@back] = element
@back = (@back + 1) % @store.length
end
end

def dequeue

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"
if @front == -1
raise ArgumentError, "Queue is empty"
end

removed = @store[@front]
@front = (@front + 1) % @store.length

if @front == @back
@front = @back = -1
end

return removed
end

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

def size
raise NotImplementedError, "Not yet implemented"
return @store[@front...@back].length
end
Comment on lines 43 to 45

Choose a reason for hiding this comment

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

This only works if @front < @back. What about if @back has wrapped around the buffer?


def empty?
raise NotImplementedError, "Not yet implemented"
end

return @front == -1
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
list = []
current = @front

while current != @back
list << @store[current]
current = (current + 1) % @store.length
end
return list.to_s
end
end
end
22 changes: 13 additions & 9 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
require_relative './linked_list.rb'

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

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

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

return element
end

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

def to_s
return @store.to_s
end
Expand Down
50 changes: 23 additions & 27 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,66 @@
describe "Test Queue Implementation" do
it "creates a Queue" do
q = Queue.new
q.class.must_equal Queue
_(q.class).must_equal Queue
end

it "adds something to an empty Queue" do
skip
q = Queue.new
q.enqueue(10)
q.to_s.must_equal "[10]"
_(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)
q.enqueue(30)
q.to_s.must_equal "[10, 20, 30]"
_(q.to_s).must_equal "[10, 20, 30]"
end

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

it "removes something from the Queue" do
skip
q = Queue.new
q.enqueue(5)
removed = q.dequeue
removed.must_equal 5
q.empty?.must_equal true
_(removed).must_equal 5
_(q.empty?).must_equal true
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
q.enqueue(7)
removed = q.dequeue
removed.must_equal 5
q.to_s.must_equal "[3, 7]"
_(removed).must_equal 5
_(q.to_s).must_equal "[3, 7]"
end

it "properly adjusts the size with enqueueing and dequeueing" do
skip
q = Queue.new
q.empty?.must_equal true
_(q.empty?).must_equal true
q.enqueue(-1)
q.enqueue(-60)
q.empty?.must_equal false
_(q.empty?).must_equal false
q.dequeue
q.dequeue
q.empty?.must_equal true
_(q.empty?).must_equal true
end

it "returns the front element in the Queue" do
skip
q = Queue.new
q.enqueue(40)
q.enqueue(22)
q.enqueue(3)
q.dequeue
expect(q.dequeue).must_equal 22
end

it "works for a large Queue" do
q = Queue.new
q.enqueue(10)
Expand All @@ -92,15 +86,17 @@
q.enqueue(130)
q.enqueue(140)
q.enqueue(150)
q.enqueue(150)
q.enqueue(160)
q.enqueue(170)
q.enqueue(180)
q.enqueue(190)
q.enqueue(200)
q.enqueue(210)
q.enqueue(220)
q.enqueue(230)
q.enqueue(240)
q.dequeue

expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240]')
end
end
33 changes: 14 additions & 19 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,43 @@
describe "Test Stack Implementation" do
it "creates a Stack" do
s = Stack.new
s.class.must_equal Stack
_(s.class).must_equal Stack
end

it "pushes something onto a empty Stack" do
skip
s = Stack.new
s.push(10)
s.to_s.must_equal "[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)
s.push(30)
s.to_s.must_equal "[10, 20, 30]"
_(s.to_s).must_equal "[10, 20, 30]"
end

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

it "removes something from the stack" do
skip
s = Stack.new
s.push(5)
removed = s.pop
removed.must_equal 5
s.empty?.must_equal true
_(removed).must_equal 5
_(s.empty?).must_equal true
end

it "removes the right something (LIFO)" do
skip
s = Stack.new
s.push(5)
s.push(3)
s.push(7)
removed = s.pop
removed.must_equal 7
s.to_s.must_equal "[5, 3]"
_(removed).must_equal 7
_(s.to_s).must_equal "[5, 3]"
end
end
end