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
27 changes: 26 additions & 1 deletion lib/problems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,32 @@
# Time Complexity: ?
# Space Complexity: ?
def balanced(string)
raise NotImplementedError, "Not implemented yet"
array = string.split('')
return false if array.length % 2 != 0

stack = Stack.new
array.each do |ele|
stack.push(ele)
end

array.length.times do |i|
remove = stack.pop
case remove
when '['
return false if array[i] != ']'
when ']'
return false if array[i] != '['
when '{'
return false if array[i] != '}'
when '}'
return false if array[i] != '{'
when '('
return false if array[i] != ')'
when ')'
return false if array[i] != '('
Comment on lines +16 to +28

Choose a reason for hiding this comment

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

Just a note that you can dry this up with a hash where they keys are the close braces and the values are the corresponding open braces.

return false if brace_hash[remove] != array.pop

end
end
return true
end

# Time Complexity: ?
Expand Down
51 changes: 43 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,66 @@
class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = []
@max_size = 20
@front = -1
@rear = -1
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if (@front == 0 && @rear == (@max_size - 1)) ||
(@rear == ((@front - 1) % (@max_size - 1)))
Comment on lines +11 to +12

Choose a reason for hiding this comment

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

Suggested change
if (@front == 0 && @rear == (@max_size - 1)) ||
(@rear == ((@front - 1) % (@max_size - 1)))
if (@front != -1 && @front == (@rear + 1) % @max_size

raise "No more space"
elsif @front == -1 # Queue is empty
# initialize front and rear
@front += 1
@rear += 1
elsif (@rear == (@max_size - 1) && @front != 0) # rear needs to wrap around
@rear = 0
else
@rear += 1
end
Comment on lines +14 to +22

Choose a reason for hiding this comment

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

This can be dried up with something like

Suggested change
elsif @front == -1 # Queue is empty
# initialize front and rear
@front += 1
@rear += 1
elsif (@rear == (@max_size - 1) && @front != 0) # rear needs to wrap around
@rear = 0
else
@rear += 1
end
else
@rear = (@rear + 1) % @max_size
end

@store[@rear] = element
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 @front == -1
raise "Nothing to remove"
elsif @front == @rear
dequeue = front
@front = -1
@rear = -1
else
dequeue = front
@front += 1
end
return dequeue
end

def front

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 @store[@front] if @front != -1
end

# get how many elements are in the array
def size

Choose a reason for hiding this comment

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

👍 , Nicely done

raise NotImplementedError, "Not yet implemented"
if @rear >= @front
return (@rear - @front) + 1
else
return (@max_size) - (@front - @rear) + 1
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
end

def to_s
return @store.to_s
if @front <= @rear
return @store[@front, @max_size].to_s
else
arr = @store[@front, @max_size]
arr += @store[0.. @rear]
return arr.to_s
end
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

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 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 All @@ -26,7 +26,7 @@
expect(balanced('[]')).must_equal true
expect(balanced('{}')).must_equal true
end

it "also works if the string has opens and closes in the beginning and end" do

expect(balanced('[]()')).must_equal true
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"