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

# Time Complexity: ?
# Space Complexity: ?
# time complexity: 0(n) where n is the legth
# space complexity: 0(n) - creates a new stack
def balanced(string)
raise NotImplementedError, "Not implemented yet"
return false if string.length.odd?

# create hash for quick look up
brackets = {
"(" => ")", "{" => "}", "[" => "]"
}
stack = Stack.new

string.chars.each do |char|
if brackets[char]
element = brackets[char]
stack.push(element)
else
if stack.pop != char
return false
end
end
end
return stack.empty?
end

# Time Complexity: 0(n) where n is the length
# Space Complexity: 0(n)
# def balanced(string)
# # string length must be even
# return false if string.length.odd?

# stack = Stack.new

# # loop through each char in the string
# string.each_char do |char|
# if char == "["
# stack.push("]")
# elsif char == "{"
# stack.push("}")
# elsif char == "("
# stack.push(")")
# # if current char is the same as the last inserted char on stack, remove last inserted char
# elsif !stack.empty? && stack.top == char
# stack.pop
# # if stack is empty or last char is not 'opening' pair
# else
# return false
# end
# end
# # finally if you get here and stack is empty - string is balanced
# return stack.empty?
# end


# Time Complexity: ?
# Space Complexity: ?
def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"
# optional wave 4
def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"

end
99 changes: 81 additions & 18 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,94 @@
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.

This works, but it's not what was asked. The assignment calls you to create a Queue using a circular buffer. So... yes it's a queue and passes the tests, but it wasn't implemented as requested.


def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(20)
@size = 20
@front = -1
@back = -1
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"

# add element to rear
def enqueue(value)
if @front == -1
@front = 0
@back = 1
@store[@front] = value
elsif @store.empty?
raise ArgumentError, "Full"
Comment on lines +16 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If the Queue is empty raise an argument error that it's full?

I think you want to check to see if it's full by doing if (@back + 1) % @store.length == @front

else
@store[@back] = value
@back = (@back + 1) % @size
end
end

# remove element from front and return it
def dequeue
raise NotImplementedError, "Not yet implemented"
end

def front
raise NotImplementedError, "Not yet implemented"
if @front == -1
raise ArgumentError, "Empty"
else
data = @store[@front]
@front = (@front + 1) % @size
if @store.empty?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You already checked to see if it's empty above with if @front == -1

@front = back = -1
end
return data
end
end

def size
raise NotImplementedError, "Not yet implemented"
end


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

def to_s
return @store.to_s
list = []
current = @front

while current != @back
list << @store[current]
current = (current + 1) % @size
end
return list.to_s
end
end

# class Queue
# passes all tests but not made with a circular buffer.
# attr_accessor :front, :rear, :store
# attr_reader :size

# def initialize(size=20)
# @size = size
# @store = Array.new()
# @front = 0
# @rear = -1
# end

# # add element to rear
# def enqueue(element)
# return @store.push(element)
# end

# # remove element from front
# def dequeue
# return nil if @store.empty?
# return @store.shift()
# end

# def front(queue)
# return @front == -1
# end

# def size
# return @store.size
# end

# def empty?
# if @front == 0 && @rear == -1
# return @store.empty?
# end
# end

# def to_s
# return @store.to_s
# end
# end
15 changes: 9 additions & 6 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"
@store.remove_first
end

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

def top
@store.get_first
end

def to_s
Expand Down
4 changes: 2 additions & 2 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 @@ -33,7 +33,7 @@
end
end

describe "postfix" do
xdescribe "postfix" do
it "can add a 2 numbers together" do

expect(evaluate_postfix("34+")).must_equal 7
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"