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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)

def balanced(string)
Comment on lines +3 to 6

Choose a reason for hiding this comment

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

👍 Thanks for documenting your source!

raise NotImplementedError, "Not implemented yet"
end
# reference https://www.youtube.com/watch?v=0OJdfXyuapc
stack = Stack.new
parens = string.split('')
i = 0

# Time Complexity: ?
# Space Complexity: ?
def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"
while i < parens.length
if (parens[i] == "(" || parens[i] == "{" || parens[i] == "[")
stack.push(parens[i])
elsif (parens[i] == ")" || parens[i] == "}" || parens[i] == "]")
value = stack.pop

case parens[i]
when ")"
if (value == "{" || value == "[")
return false
end
when "}"
if (value == "(" || value == "[")
return false
end
when "]"
if (value == "(" || value == "{")
return false
end
end
end
i += 1
end
return stack.empty? ? true : false
end

# keeps giving 5 erros :(
# open_parens = [ '(', '[', '{' ]
# close_parens = [')', ']', '}' ]

# check if string length
# if string.length % 2 != 0
# return false
# # check if string is empty
# elsif string.length == 0
# return true
# else
# # check if string starts with a close paren or ends with an open paren (unbalanced)
# if (close_parens.include? string[0]) || (open_parens.include? string[-1])
# return false
# else
# stack = Stack.new
# end
# end

# string.each_char do |paren|
# if open_parens.include? paren
# stack.push(paren)
# # if paren is a close paren, remove last open paren in stack and check to ensure it's a match for the close paren paren.
# elsif close_parens.include? paren
# last_in_stack = stack.pop()
# if last_in_stack != open_parens[close_parens.index(paren)]
# return false
# end
# end
# end
# return stack.empty?
102 changes: 94 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,117 @@
class Queue

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

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if ((@front == 0 && @back == @size - 1) || (@back == (( @front - 1) % ( @size - 1))))

Choose a reason for hiding this comment

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

I think this looks a little simpler

Suggested change
if ((@front == 0 && @back == @size - 1) || (@back == (( @front - 1) % ( @size - 1))))
if ((@front == 0 && @back == @size - 1) || (@front == (( @back + 1) % ( @size))))

raise ArgumentError
elsif (@front == - 1)
@front = 0
@back = 0
@store[@back] = element
elsif ((@back == @size - 1) && (@front != 0))
@back = 0
@store[@back] = element
else
@back = @back + 1
@store[@back] = element
end

# if @front == @back && !@empty
# raise ArgumentError
# else
# @store[@back] = element
# @empty = false
# @back = (@back + 1) % @store.length
# end
end

def dequeue
raise NotImplementedError, "Not yet implemented"
if (@front == - 1)
raise ArgumentError
end

new_queue = @store[@front]
@store[@front] = nil

if (@front == @back)
@front = -1
@back = -1
elsif (@front == @size - 1 )
@front = 0
else
@front += 1
end
Comment on lines +47 to +51

Choose a reason for hiding this comment

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

Suggested change
elsif (@front == @size - 1 )
@front = 0
else
@front += 1
end
else
@front = (@front + 1) % @size
end


return new_queue

# value = @store[@front]
# @store[@front] = nil
# @front = (@front + 1) % @store.length

# if @front == @back
# @empty = true
# end

# return value
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.

👍

raise NotImplementedError, "Not yet implemented"
if @back < @front
count = @size - @front + @back + 1
else
count = @back - @front + 1
end
return count

# if @front < @back
# return @back - @front
# else
# return @back + @store.length - @front
# end
end

def empty?

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"
return ((@front == - 1) && (@back == - 1))

# return @empty
end

def to_s
return @store.to_s

if @front == -1 && @back == -1
return '[]'
elsif @front <= @back
return @store[@front..@back].to_s
else
i = 0
index = @front
size = @size - @front + @back + 1
result = []

while i < size
result << @store[index]
if index < @size - 1
index += 1
else
index = 0
end
i += 1
end

return result.to_s
end
end
# return @store.to_s
end
14 changes: 9 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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"
if @store.nil?
@store.add_first(element)
else
@store.add_last(element)
end
end

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

def empty?
raise NotImplementedError, "Not yet implemented"
return @store.empty?
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