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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - goes through all input elements
# Space Complexity: O(n) - stack will take at least half of symbols in string
def balanced(string)
Comment on lines +3 to 5
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"
return true if string.length == 0

pairs = {"(" => ")", "{" => "}", "[" => "]"}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I love the use of a hash here!

expectations = Stack.new
string.chars.each do |char|
if pairs[char]
expected_element = pairs[char]
expectations.push(expected_element)
else
if expectations.pop != char
return false
end
end
end
return expectations.empty?
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - goes through all input elements
# Space Complexity: O(1) - stack will never have more than 2 elements
def evaluate_postfix(postfix_expression)
Comment on lines +23 to 25
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"
end
operands = Stack.new
postfix_expression.chars.each do |element|
if element.to_i.to_s == element
operands.push(element.to_i)
else
num2 = operands.pop
num1 = operands.pop
case element
when "+"
answer = num1 + num2
when "*"
answer = num1 * num2
when "-"
answer = num1 - num2
when "/"
answer = num1 / num2
end
operands.push(answer)
end
end
result = operands.pop
if operands.empty?
return result
else
raise Exception.new "Expression is not valid"
end
end
39 changes: 31 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
class Queue
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍 , Well done, good work implementing a circular buffer!


def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new
@size = 20
@start = 0
@end = 0
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
# check if we can add more elements
raise Exception.new "No vacant spots in queue" if @size < self.size

@store[@end] = element
@end = (@end + 1) % @size
end

def dequeue
raise NotImplementedError, "Not yet implemented"
raise Exception.new "No elements in queue" if self.size == 0
element = @store[@start]
@store[@start] = nil
@start = (@start + 1) % @size
return element
end

def front
raise NotImplementedError, "Not yet implemented"
raise Exception.new "No elements in queue" if self.size == 0
@store[@start]
end

def size
raise NotImplementedError, "Not yet implemented"
if @end == @start && @store[@start]
return 20
elsif @end > @start
return @end - @start
elsif @end < @start
return @end + @size - @start
else
return 0
end
end

def empty?
raise NotImplementedError, "Not yet implemented"
@end == @start
end

def to_s
return @store.to_s
if @end > @start
return @store[@start..@end].to_s
else
return (@store[@start..@size-1] + @store[0..@end-1]).to_s
end
end
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 './linked_list.rb'

class Stack
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"