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
54 changes: 48 additions & 6 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,55 @@
require_relative './stack.rb'

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
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.

👍

raise NotImplementedError, "Not implemented yet"
braces_stack = Stack.new
string.each_char do |n|
case n
when "("
braces_stack.push(n)
when "{"
braces_stack.push(n)
when "["
braces_stack.push(n)
when ")"
return false if !stack_checker(braces_stack, "(")
when "}"
return false if !stack_checker(braces_stack, "{")
when "]"
return false if !stack_checker(braces_stack, "[")
end
end
return braces_stack.empty?
end

# Time Complexity: ?
# Space Complexity: ?
def stack_checker(stack, expected_paren)
return !(stack.empty? || stack.pop != expected_paren)
end

# Time Complexity: O(n)
# Space Complexity: O(n) <- but we'd probably use less assuming the expression contains operators
def evaluate_postfix(postfix_expression)
Comment on lines +30 to 32

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Not implemented yet"
stack = Stack.new
postfix_expression.each_char do |n|
if /\+|-|\*|\//.match(n)
stack_operation_helper(stack, stack.pop, stack.pop, n)
else
stack.push(n)
end
end
return stack.pop
end

def stack_operation_helper(stack, first_pop, second_pop, operator)
case operator
when "+"
stack.push(second_pop.to_i + first_pop.to_i)
when "-"
stack.push(second_pop.to_i - first_pop.to_i)
when "*"
stack.push(second_pop.to_i * first_pop.to_i)
when "/"
stack.push(second_pop.to_i / first_pop.to_i)
end
end
65 changes: 49 additions & 16 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,64 @@
class Queue

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

def enqueue(element)

Choose a reason for hiding this comment

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

This method could use being broken into some helper methods. Otherwise it seems to work.

raise NotImplementedError, "Not yet implemented"
#resize if the array is full
if @front == (@back + 1) % @store.length
temp = Array.new(@store.length + 1)

Choose a reason for hiding this comment

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

Why make it one bigger? That gets pretty expensive in terms of time.

if @front > @back
temp.each_index do |n|
if n < @front
temp[n] = @store[n]
elsif n > @front
temp[n] = @store[n-1]
end
end
@front += 1
else
temp.each_index do |n|
temp[n] = @store[n]
end
end
@store = temp
end
#add the element
@store[@back] = element
@back = (@back + 1) % @store.length
end

def dequeue
raise NotImplementedError, "Not yet implemented"
end

if @front != @back
temp = @store[@front]
@store[@front] = nil
@front = (@front + 1) % @store.length
return temp
end
end

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

def size
raise NotImplementedError, "Not yet implemented"
return abs(@back - @front + 1)

Choose a reason for hiding this comment

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

This only works if @back > @front

end

def empty?
raise NotImplementedError, "Not yet implemented"
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
temp = []
curr = @front
until curr == @back
temp << @store[curr] if @store[curr]
curr = (curr + 1) % @store.length
end
return "#{temp}"
end
end
11 changes: 6 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
require_relative '../lib/linked_list'

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"
return @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
59 changes: 30 additions & 29 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,67 @@
describe "Test Queue Implementation" do
it "creates a Queue" do
q = Queue.new
q.class.must_equal Queue
expect(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]"
expect(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]"
expect(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 "a Queue is empty after removing all the elements" do
q = Queue.new
q.enqueue(5)
q.enqueue(6)
expect(q.dequeue).must_equal 5
expect(q.dequeue).must_equal 6
expect(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)
Expand All @@ -92,15 +94,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
end
21 changes: 8 additions & 13 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