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

# 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"
stack = Stack.new
string.each_char do |char|
if char == "(" || char == "[" || char == "{"
stack.push(char)
elsif char == ")" || char == "]" || char == "}"
last_pushed = stack.pop()
if last_pushed == "(" && char != ")" ||
last_pushed == "[" && char != "]" ||
last_pushed == "{" && char != "}"
return false
end
end
end

# check if stack is full of opening braces
return stack.empty?
end

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

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 = Stack.new
postfix_expression.each_char do |char|
if /[0-9]/.match(char)
stack.push(char.to_i)
elsif /[\+\-\/\*]/.match(char)
second_operand = stack.pop()
first_operand = stack.pop()
stack.push(first_operand.send(char, second_operand))
end
end

return stack.pop()
end
42 changes: 34 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,57 @@
class Queue

ARRAY_SIZE = 20

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(ARRAY_SIZE)
@front = @back = 0
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if self.full?
raise NoMemoryError, "Array is full"
else
@store[@back] = element
@back = (@back + 1) % ARRAY_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 self.empty?
raise IndexError, "Array is empty"
else
dequeued_element = @store[@front]
@store[@front] = nil
@front = (@front + 1) % ARRAY_SIZE
return dequeued_element
end
end

def front
raise NotImplementedError, "Not yet implemented"
return @store[@front]
end

def size

Choose a reason for hiding this comment

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

👍 nice and compact

raise NotImplementedError, "Not yet implemented"
return @front < @back ? @back - @front : @store.length + @back - @front
end

def empty?
raise NotImplementedError, "Not yet implemented"
return @front == @back
end

def full?

Choose a reason for hiding this comment

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

👍

return @front == (@back + 1) % ARRAY_SIZE
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
string = "["
i = @front
until i == @back - 1
string << "#{@store[i]}, "
i = (i + 1) % ARRAY_SIZE
end

string << "#{@store[@back - 1]}]"
return string
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
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()
end

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

def to_s
Expand Down
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'

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