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

# 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"
strStack = Stack.new
string.each_char do |char|
if ['(', '[', '{'].include?(char)
strStack.push(char)
else
element = strStack.pop
if char == ']' && element != '['
return false
elsif char == ')' && element != '('
return false
elsif char == '}' && element != '{'
return false
end
Comment on lines +12 to +18

Choose a reason for hiding this comment

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

This could be dried up using a hash with keys being the open brace and the value being the corresponding close brace.

end
end


return strStack.empty?
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(N)
# Space Complexity: O(N)
def evaluate_postfix(postfix_expression)
Comment on lines +26 to 28

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"
postfix_stack = Stack.new

postfix_expression.each_char do |char|
if numeric?(char)
postfix_stack.push(char.to_i)
else

num2 = postfix_stack.pop
num1 = postfix_stack.pop

case char
when "+"
answer = num1 + num2
when "*"
answer = num1* num2
when "-"
answer = num1- num2
when "/"
answer = num1/ num2
end

if answer== nil
postfix_stack.push(num2)
postfix_stack.push(num1)
else
postfix_stack.push(answer)
answer = nil
end
end
end

return postfix_stack.pop

end

def numeric?(char)
char =~ /[[:digit:]]/
end
21 changes: 13 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
require_relative "linked_list"

class Queue

Choose a reason for hiding this comment

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

This works, but isn't implemented with a circular buffer. So it's not what's asked for.


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

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
@list.add_last(element)
end

def dequeue
raise NotImplementedError, "Not yet implemented"
return nil if self.empty?

element = @list.remove_first

return element
end

def front
raise NotImplementedError, "Not yet implemented"
@list.get_first
end

def size
raise NotImplementedError, "Not yet implemented"
@list.length
end

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

def to_s
return @store.to_s
return @list.to_s
end
end
18 changes: 12 additions & 6 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
require_relative "linked_list"

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"
return nil if self.empty?

element = @list.remove_first

return element

end

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

def to_s
return @store.to_s
return @list.to_s
end
end
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
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
require_relative "../lib/linked_list.rb"
require_relative "../lib/queue.rb"
require_relative "../lib/stack.rb"
require_relative "../lib/problems.rb"
# Extra exercises
# require_relative "../lib/problems.rb"