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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(nlogn)
# 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.

Since you have to go through all the chars and you're not splitting at each stage, this is O(n) for time complexity.

raise NotImplementedError, "Not implemented yet"
stack = Stack.new
arr = string.split('')
chars = ['(', '[', '{']
arr.each do |char|
if chars.include?(char)
stack.push(char)
else
Comment on lines +10 to +12

Choose a reason for hiding this comment

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

Could you do this with a hash where the keys are the open brace and he values are the matching close brace? That might help you dry up those if-else statements.

current = stack.pop
if current == '(' && char != ')'
return false
elsif current == '[' && char != ']'
return false
elsif current == '{' && char != '}'
return false
end
end
end
return stack.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.

👍 Interesting use of eval

raise NotImplementedError, "Not implemented yet"
stack = Stack.new
postfix_expression.each_char do |char|
if char.match(/[0-9]/)
stack.push(char.to_i)
else
a = stack.pop
b = stack.pop
stack.push(eval "#{b}#{char}#{a}")
end
end
return stack.pop
end
52 changes: 43 additions & 9 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,65 @@
class Queue

Choose a reason for hiding this comment

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

👍 Well done!


def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
attr_accessor :start, :size, :start, :end

def initialize(size=20)
@store = Array.new(size)
@size = size
@start, @end = -1, -1
end

def full?
(@end + 1) % @size == @start
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"
# raise ArgumentError.new("Queue full") if self.full?

if self.empty?
@start = 0
@end = 1
@store[@start] = element
elsif @start == @end
raise ArgumentError.new("Queue full!")
else
@store[@end] = element
@end = (@end + 1) % @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 @start == -1
raise ArgumentError.new("Queue empty")
else
element = @store[@start]
@start = (@start + 1) % @size
if @start == @end
@start, @end = -1
end
end
return element
end

def front
raise NotImplementedError, "Not yet implemented"
raise ArgumentError.new("Queue empty") if @start == -1
@start
end

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

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

def to_s
return @store.to_s
arr = []
current = @start
while current != @end
arr << @store[current]
current = (current+1) % size
end
return arr.to_s
end
end
12 changes: 7 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
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"
@store = LinkedList.new
end

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

def pop
raise NotImplementedError, "Not yet implemented"
@store.remove_last if [email protected]?
end

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

def to_s
return @store.to_s
end
end

3 changes: 2 additions & 1 deletion test/problems_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require_relative 'test_helper'
require_relative '../lib/problems.rb'

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