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

Sara - Branches #35

Open
wants to merge 7 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
2 changes: 1 addition & 1 deletion lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def remove_first()

value = @head.data
@head = @head.next
@head.previous = nil
# @head.previous = nil
return value
end

Expand Down
2 changes: 2 additions & 0 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Wave 3
require_relative './stack.rb'

# Time Complexity: ?
Expand All @@ -6,6 +7,7 @@ def balanced(string)
raise NotImplementedError, "Not implemented yet"
end

# Wave 4
# Time Complexity: ?
# Space Complexity: ?
def evaluate_postfix(postfix_expression)
Expand Down
47 changes: 40 additions & 7 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
# Wave 2
# require_relative './stack.rb'
require "./linked_list"

Choose a reason for hiding this comment

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

This should be a require_relative, although you don't need a linked list in this class for the assignment.


class Queue

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

Choose a reason for hiding this comment

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

⚠️ You were asked to use a circular buffer instead of a LinkedList for this class.

These work, but using a LinkedList wasn't the assignment.

@front = @back = -1
# @front = -1
# @back = -1
end

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

if @front == @back
raise ArgumentError.new("Error")
end

return @store.add_last(element)

end

def dequeue
raise NotImplementedError, "Not yet implemented"
# if @front == -1 && @back == -1
# @front = 0
# @back = 1
# end

if @store.length > 0
return @store.remove_first()
end

# puts @store
# puts @front
# puts @back

end

def front
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
return @store.get_first()
end

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

def empty?
raise NotImplementedError, "Not yet implemented"
# raise NotImplementedError, "Not yet implemented"
# return true if @front == @back
return @store.empty?

end

def to_s
Expand Down
12 changes: 7 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# wave 1
require "./linked_list"

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

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

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

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

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

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

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

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

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

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

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

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

it "returns the front element in the Queue" do
skip
# skip
q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand All @@ -84,6 +86,7 @@
expect(q.dequeue).must_equal 22
end
it "works for a large Queue" do
# skip
q = Queue.new
q.enqueue(10)
q.enqueue(20)
Expand Down Expand Up @@ -111,6 +114,6 @@
q.enqueue(210)
q.dequeue

expect(q.to_s).must_equal('[30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]')
expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 150, 160, 170, 180, 190, 200, 210]')
end
end
10 changes: 5 additions & 5 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
end

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

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

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

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

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