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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) as it needs to go through all the string
# Space Complexity: O(n) in worst scenario all parenthesis will be open.
def balanced(string)
raise NotImplementedError, "Not implemented yet"
return true if string == ""
stack = []

pairs = Hash.new()
pairs["}"] = "{"
pairs[")"] = "("
pairs["]"] = "["

string.each_char do |c|
if ["{","[","("].include?(c)
stack.push(c)
else
return false if stack.pop() != pairs[c]
end
end

return stack.empty?
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n). It needs to iterate through each character.
# Space Complexity: O(n). In the worst scenario I will only have operands.
def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"

stack = []

postfix_expression.each_char do |c|
if ["+","-","/","*"].include?(c)
n2 = (stack.pop).to_i
n1 = (stack.pop).to_i

if c == "+"
result = n1 + n2
elsif c == "-"
result = n1 - n2
elsif c == "*"
result = n1 * n2
elsif c == "/"
result = n1 / n2
end

stack.push(result)
else
num = c.to_i
stack.push(num)
end
end
return stack.pop
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
3 changes: 3 additions & 0 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

describe "Test Queue Implementation" do
it "creates a Queue" do
skip
q = Queue.new
expect(q.class).must_equal Queue
end
Expand Down Expand Up @@ -84,6 +85,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 @@ -115,6 +117,7 @@
end

it "works for a large queue with a large number of adds and removes" do
skip
q = Queue.new

99.times do
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