Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
42 changes: 36 additions & 6 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
require_relative './stack.rb'

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n*m)
# Space Complexity: O(m), but also potentially O(1)
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.

👍 Since you're building a stack it can't be O(1)

raise NotImplementedError, "Not implemented yet"
# assumes these are only valid chars
hash_table = {"}" => "{", "]" => "[", ")" => "("} # space: O(m)? maybe O(1) bc size of this doesnt grow with input?
stack = Stack.new

string.each_char do |char| # time: O(n)
if hash_table.has_value?(char) # time: O(m)?

Choose a reason for hiding this comment

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

I don't think you need to use has_value here.

I think better something like this

matching_braces = {
  "}" => "{",
  ")" => "(",
  "]" => "[",
} 
stack = Stack.new
string.each_char do |char|
  if ! matching_braces[char]
    stack.push(char)
  elsif matching_braces[char] != stack.pop() 
    return false
  end
end
return stack.empty()

stack.push(char)
else
return false if stack.pop != hash_table[char] # time: O(1)
end
end

return stack.empty?
end

# Time Complexity: ?
# Space Complexity: ?

# Time Complexity: O(n)
# Space Complexity: O(m), but also potentially O(1)
def evaluate_postfix(postfix_expression)
Comment on lines +22 to 25

Choose a reason for hiding this comment

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

👍 , but the space and time complexities are O(n)

raise NotImplementedError, "Not implemented yet"
# assumes these are only valid operations
operations = {"*" => true, "+" => true, "/" => true, "-" => true} # space: O(m), maybe O(1) bc size of this doesnt grow with input?
stack = Stack.new
result = ""

postfix_expression.each_char do |char| # time: O(n)
if operations[char] # time: O(1)
num2 = stack.pop.to_i # time: O(1)
num1 = stack.pop.to_i # time: O(1)
result = num1.method(char).(num2)
stack.push(result) # time: O(1), space: O(1)?? because will never be larger than 3 elements
else
stack.push(char) # time: O(1)
end
end

return result

end
49 changes: 40 additions & 9 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
class Queue

SIZE = 20

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

def enqueue(element)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Not yet implemented"

if @front == -1 && @back == -1
@front = @back = 0
elsif (@back + 1) % SIZE == @front
raise ArgumentError.new("Queue is full")
elsif @back == SIZE - 1
@back = 0
else
@back += 1
end

@store[@back] = element
end

def dequeue

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Not yet implemented"
raise ArgumentError, "Queue is empty" if @store.empty?

result = @store[@front]
@store[@front] = nil
if @front == @back
@front = @back = -1
elsif @front == SIZE - 1
@front = 0
else
@front += 1
end
return result
end

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

def size
raise NotImplementedError, "Not yet implemented"
return @store.select{|x| !x.nil?}.length
end

def empty?
raise NotImplementedError, "Not yet implemented"
@store.map{|x| x.nil?}.all?
end
Comment on lines 40 to 50

Choose a reason for hiding this comment

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

👍


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
queue = []
20.times do |i|
index = (i + @front) % SIZE
queue.push(@store[index]) if @store[index]
end

return queue.to_s
end
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

Choose a reason for hiding this comment

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

👍

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

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

def pop
raise NotImplementedError, "Not yet implemented"
@store.remove_first
end

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

def to_s
Expand Down
2 changes: 1 addition & 1 deletion test/problems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

xdescribe "Test wave 3 problems" do
describe "Test wave 3 problems" do
describe "balanced" do
it "Given balanced strings it should return true" do

Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
require_relative "../lib/queue.rb"
require_relative "../lib/stack.rb"
# Extra exercises
# require_relative "../lib/problems.rb"
require_relative "../lib/problems.rb"