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

Niv #19

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

Niv #19

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
21 changes: 20 additions & 1 deletion lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
require_relative './stack.rb'

def balanced(string)
raise NotImplementedError, "Not implemented yet"
pairs = {")" => "(", "}" => "{", "]"=>"["}

Choose a reason for hiding this comment

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

Great use of a hash!

character_stack = Stack.new
i = 0
while i < string.length
if pairs.has_value?(string[i]) == true
character_stack.push(string[i])
else
if character_stack.pop != pairs[string[i]]
return false
end
end
i+=1
end

if character_stack.empty? == true
return true
else
return false
end

end

def evaluate_postfix(postfix_expression)
Expand Down
55 changes: 46 additions & 9 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@BUFFER_SIZE = 10
@store = []
@front = -1
@rear = -1
end

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

Choose a reason for hiding this comment

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

This could run into trouble if the Queue becomes full and the front is not at index 0.

Consider if you had a buffer size of 10,

You add 9 elements, remove one (so front is at index 1) and then try to add 7 more. What happens?

raise Error, "Queue is Full"
elsif @front == -1
@front = 0
@rear = 0
@store[@rear] = element
elsif @rear == @store.length - 1 && @front != 0
@rear = 0
@store[@rear] = element
else
@rear += 1
@store[@rear] = element
end
end

def dequeue
raise NotImplementedError, "Not yet implemented"
removed_data = @store[@front]

Choose a reason for hiding this comment

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

Before dequeuing you should check to see if the queue is empty.

@store[@front] = nil
if (@front == @rear)
@front = -1
@rear = -1
elsif @front === @BUFFER_SIZE - 1

Choose a reason for hiding this comment

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

This kind of elsif statements work or you could do: @front = (@front + 1) % BUFFER_SIZE

@front = 0
else
@front += 1
end
return removed_data
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 == -1
return true
end
return false
end

def to_s
return @store.to_s
display_store = []
if @front <= @rear
display_store = @store[@front...@rear+1]
else
first_half = @store[0...@rear]
second_half = @store[@[email protected]+1]

Choose a reason for hiding this comment

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

I don't think this actually works.

display_store.push(first_half)
display_store.push(second_half)
end
return display_store.to_s
end
end

end
16 changes: 11 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
require "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"
removed = @store.remove_last
return removed
end

def empty?
raise NotImplementedError, "Not yet implemented"
if @store.length > 0
return false
else
return true
end
end

def to_s
Expand Down
8 changes: 4 additions & 4 deletions test/problems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
describe "Test wave 3 problems" do
describe "balanced" do
it "Given balanced strings it should return true" do
skip

expect(balanced('(({}))')).must_equal true
end

it "regards an empty string as balanced" do
skip

expect(balanced('')).must_equal true
end

it "will return false for an unbalanced set of parens" do
skip

expect(balanced('(()')).must_equal false
expect(balanced('(()}')).must_equal false
expect(balanced('([]]')).must_equal false
end

it "also works for {} and []" do
skip

expect(balanced('[]')).must_equal true
expect(balanced('{}')).must_equal true
end
Expand Down
13 changes: 6 additions & 7 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
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)
Expand All @@ -27,13 +26,13 @@
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
Expand All @@ -42,7 +41,7 @@
end

it "removes the right something (LIFO)" do
skip

q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -53,7 +52,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 @@ -65,7 +64,7 @@
end

it "returns the front element in the Queue" do
skip

q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand Down
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

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 +26,13 @@
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 +41,7 @@
end

it "removes the right something (LIFO)" do
skip

s = Stack.new
s.push(5)
s.push(3)
Expand Down