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
73 changes: 67 additions & 6 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,74 @@
# frozen_string_literal: true

require_relative './stack.rb'

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# # Space Complexity: O(n)
# def balanced(string)
# opening_brackets = Stack.new
# # string.each_char do |char|
# # # if an opening bracket, push it into the opening_brackets
# # if char == '{' || char == '(' || char == '['
# # opening_brackets.push(char)
# # else

# # compare if the last opening bracket matches with the current closing bracket in char
# opening_bracket = opening_brackets.pop()
# if matching_braces == '}' && opening_bracket != '{'
# return false
# elsif matching_braces == ')' && opening_bracket != '('
# return false
# elsif matching_braces == ']' && opening_bracket != '['
# return false
# end

# # if everything matches/closes, then we have a balanced brackets
# return opening_brackets.empty?
# end

def balanced(string)
raise NotImplementedError, "Not implemented yet"
matching_hash = {
'{' => '}',
'(' => ')',
'[' => ']'
}
matching_stacks = Stack.new

string.each_char do |char|
if matching_hash.key? char
matching_stacks.push(char)
elsif matching_hash.value? char
closing_brace = matching_hash[matching_stacks.pop]
return false if closing_brace != char
end
end
matching_stacks.empty?
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def evaluate_postfix(postfix_expression)
Comment on lines +48 to 50

Copy link
Copy Markdown

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"
operands = Stack.new
postfix_expression.each_char do |char|
if char != '+' && char != '*' && char != '-' && char != '/'
operands.push(char)
else # if not pop 2 values
y = operands.pop.to_i
x = operands.pop.to_i
result = 0

if char == '+'
result = x + y
elsif char == '*'
result = x * y
elsif char == '-'
result = x - y
elsif char == '/'
result = x / y
end

operands.push(result)
end
end
operands.pop
end
48 changes: 31 additions & 17 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
class Queue
# frozen_string_literal: true

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
class Queue
def initialize(size = 30)
@store = []
@size = size
@front = 0
@back = 0
end

def enqueue(element)

Copy link
Copy Markdown

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"
end

def dequeue
raise NotImplementedError, "Not yet implemented"
if @front == (@back + 1) % @size
raise StandardError, 'Queue is full.'
else
@store[@back] = element
@back = (@back + 1) % @size
end
end

def front
raise NotImplementedError, "Not yet implemented"
def empty?
@front == @back # front & back are pointing to the same position, then we've read everything
end

def size
raise NotImplementedError, "Not yet implemented"
end
def dequeue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

return nil if empty?

def empty?
raise NotImplementedError, "Not yet implemented"
element = @store[@front]
@front = (@front + 1) % @size # advance the front pointer/index by 1
element
end

def to_s

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

return @store.to_s
return [] if empty?

queue = []
current = @front

while current != @back
queue << @store[current]
current = (current + 1) % @size
end

queue.to_s
end
end
17 changes: 10 additions & 7 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
class Stack

Copy link
Copy Markdown

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

element = @store.remove_first

return element
end

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

def to_s
Expand Down
22 changes: 11 additions & 11 deletions 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 Expand Up @@ -36,20 +36,20 @@
describe "postfix" do
it "can add a 2 numbers together" do

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
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

expect(evaluate_postfix("34+2*")).must_equal 14
expect(evaluate_postfix("34*2/")).must_equal 6
expect(evaluate_postfix("34-1+")).must_equal 0
expect(evaluate_postfix("34/7-")).must_equal(-7)
expect(evaluate_postfix("35+6*")).must_equal 48
expect(evaluate_postfix("62/5+")).must_equal 8
expect(evaluate_postfix('34+2*')).must_equal 14
expect(evaluate_postfix('34*2/')).must_equal 6
expect(evaluate_postfix('34-1+')).must_equal 0
expect(evaluate_postfix('34/7-')).must_equal(-7)
expect(evaluate_postfix('35+6*')).must_equal 48
expect(evaluate_postfix('62/5+')).must_equal 8
end
end
end
1 change: 1 addition & 0 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
it "pushes something onto a empty Stack" do
s = Stack.new
s.push(10)
s.empty?.must_equal false
expect(s.pop).must_equal 10
end

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"