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 - Mariya #38

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
54 changes: 40 additions & 14 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,57 @@
class Queue

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

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if @front == -1 && @back == -1
@front = 0
@back = 0
end

if @front == @back && @store.compact.length > 0
front_array = @store.slice(0, @front)
back_array = @store.slice(@back, @store.length - 1)
@store = front_array + Array.new(10) + back_array
@front = (@front + 10) % @store.length
end

Comment on lines +17 to +22

Choose a reason for hiding this comment

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

This assumes that front is less than back

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

def dequeue
raise NotImplementedError, "Not yet implemented"
removed = @store[@front]
@store[@front] = nil
@front = (@front + 1) % @store.length
return removed
Comment on lines +28 to +31

Choose a reason for hiding this comment

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

You should also check if the queue is now empty.

end

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

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

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

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
if @front >= @back
return (@store[@front..-1] + @store[0...@back]).to_s
else
return @store[@front...@back].to_s
end
end
end
19 changes: 11 additions & 8 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
require_relative './linked_list'

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

def push(element)
raise NotImplementedError, "Not yet implemented"
# add to end of linked list
@store.add_last(element)
end

def pop
raise NotImplementedError, "Not yet implemented"
@store.remove_last
end

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

def to_s
return @store.to_s
end
Expand Down
27 changes: 10 additions & 17 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,35 @@
q = Queue.new
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]"
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]"
end

it "starts the size of a Queue at 0" do
skip
q = Queue.new
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
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -51,9 +46,8 @@
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.enqueue(-1)
Expand All @@ -63,16 +57,16 @@
q.dequeue
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,14 @@
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.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]')
expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210]')
end
end
15 changes: 5 additions & 10 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,35 @@
s = Stack.new
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]"
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]"
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
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)
Expand Down