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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - where n is the number of characters in the string
# Space Complexity: O(n) - worse case, all characters are openers and we push them all into the stack.
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"
brackets = {
"(" => ")",
"{" => "}",
"[" => "]"
}

openers = brackets.keys
closers = brackets.values

opener_stack = []

string.each_char do |char|
if openers.include?(char)
opener_stack.push(char)
end

if closers.include?(char)
if opener_stack.empty?
return false
else
removed_opener = opener_stack.pop
if char != brackets[removed_opener]
return false
end
end
end
end

return opener_stack.empty?

end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - where n is the number of characters in the expression
# Space Complexity: O(n) - We're using a stack to store values.
def evaluate_postfix(postfix_expression)
Comment on lines +38 to 40

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 = []
postfix_expression.each_char do |element|
if element == element.to_i.to_s
stack.push(element.to_i)
else
b = stack.pop
a = stack.pop
case element
when "+"
stack.push(a + b)
when "-"
stack.push(a - b)
when "*"
stack.push(a * b)
when "/"
stack.push(a / b)
end
end
end
return stack.pop
end


47 changes: 38 additions & 9 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,60 @@
class Queue

MAX_SIZE = 20
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@list = Array.new(MAX_SIZE)
@front = -1
@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 (@back == MAX_SIZE - 1 && @front == 0) || (@back == (@front - 1) % (MAX_SIZE - 1))
raise "queue is full"
elsif @front == -1 # queue is empty?
@front = 0
@back = 1
@list[@front] = element
else
@list[@back] = element
@back = (@back + 1) % MAX_SIZE
end

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"

if @front == -1
raise "queue is empty"
end

data = @list[@front]

if @front == @back # if the queue is now empty
@front = -1
@back = -1
else
@front = (@front + 1) % MAX_SIZE
end

return data
end

def front

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"
return @list[@front]
end

def size

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"
return @front == -1 ? 0 : @back - @front + 1
end

def empty?

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"
return @front == -1 || @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
if @front <= @back
return @list.slice(@front...@back).to_s
else
return (@list.slice(@front...MAX_SIZE) + @list.slice(0...@back)).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"
@list = LinkedList.new
end

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

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

def empty?
raise NotImplementedError, "Not yet implemented"
@list.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
3 changes: 1 addition & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
require_relative "../lib/linked_list.rb"
require_relative "../lib/queue.rb"
require_relative "../lib/stack.rb"
# Extra exercises
# require_relative "../lib/problems.rb"
require_relative "../lib/problems.rb"