Skip to content
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
19 changes: 18 additions & 1 deletion lib/problems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,24 @@
# Time Complexity: ?
# Space Complexity: ?
def balanced(string)
Comment on lines 3 to 5

Choose a reason for hiding this comment

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

The method works, time and space complexity?

raise NotImplementedError, "Not implemented yet"
stack = Stack.new
pairs = {
')'=> '(',
'}'=> '{',
']'=> '['
}

string.each_char do |char|
if close.keys.include?(char)
stack.push(char)
elsif close.values.include?(char)
return false if stack.empty?
element = stack.pop
return false if char != close[element]
end
end

return stack.empty?
end

# Time Complexity: ?
Expand Down
36 changes: 31 additions & 5 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
class Queue

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

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

if @front == @back
end
Comment on lines +11 to +14

Choose a reason for hiding this comment

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

Suggested change
end
if @front == @back
end
elsif @front == @back
# Raise an error if the Queue is full
end


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

def dequeue
raise NotImplementedError, "Not yet implemented"
if @front == @back
return "queue is empty"

Choose a reason for hiding this comment

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

It would be better to raise an error in this circumstance.

end

data = @store[@front]
@store[@front] = nil

if @front == @back

Choose a reason for hiding this comment

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

I think you need to advance front before comparing it to back.

@front = @back = -1
elsif @front == @store.length - 1
@front = 0
else
@front += 1
end

return data
end

def front
Expand All @@ -22,10 +45,13 @@ def size
end

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

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
end
return @front == @back

end

def to_s
@store.delete(nil)

Choose a reason for hiding this comment

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

This will shrink the internal array. It's both an expensive operation and it will also not maintain the order of elements in the Queue.

return @store.to_s
end
end
9 changes: 4 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
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"
@store.remove_last()
end

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

def to_s
Expand Down
7 changes: 0 additions & 7 deletions test/problems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,38 @@
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

it "also works if the string has opens and closes in the beginning and end" do
skip
expect(balanced('[]()')).must_equal true
end
end

describe "postfix" do
it "can add a 2 numbers together" do
skip
expect(evaluate_postfix("34+")).must_equal 7
expect(evaluate_postfix("34*")).must_equal 12
expect(evaluate_postfix("34-")).must_equal -1
expect(evaluate_postfix("34/")).must_equal 0
end

it "can add a evaluate a more complicated expression" do
skip
expect(evaluate_postfix("34+2*")).must_equal 14
expect(evaluate_postfix("34*2/")).must_equal 6
expect(evaluate_postfix("34-1+")).must_equal 0
Expand Down
7 changes: 0 additions & 7 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)
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 "removes something from the Queue" do
skip
q = Queue.new
q.enqueue(5)
removed = q.dequeue
Expand All @@ -42,7 +38,6 @@
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -53,7 +48,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 @@ -65,7 +59,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