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
30 changes: 27 additions & 3 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
require_relative './stack.rb'

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), where n is the length of the string
# Space Complexity: O(n)
def balanced(string)
Comment on lines +3 to 5

Copy link
Copy Markdown

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"
# base case: length of the expression must be even
return false if string.length.odd?

# create a new stack
stack = Stack.new

# loop through each char in the string
string.each_char do |char|
# if current char is an opening brace, push the corresponding closing brace to the stack.
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
else # if stack is empty or last inserted char is not an opening brace
return false
end
end

# string is balanced only if stack is empty at this point
return stack.empty?
end

# Time Complexity: ?
Expand Down
61 changes: 47 additions & 14 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,64 @@
class Queue

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

def enqueue(element)

Copy link
Copy Markdown

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 == 0 && @rear == @size - 1) ||
(@rear == (@front - 1) % (@size - 1))
raise RuntimeError.new("Error: Queue is full")
elsif @front == -1 # queue is empty
@front = @rear = 0
@store[@rear] = element
elsif @rear == @size - 1 && @front != 0 # rear needs to wrap around
@rear = 0
@store[@rear] = element
else
@rear += 1
@store[@rear] = element
end
Comment on lines +17 to +23

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 could be simplified a bit.

Suggested change
elsif @rear == @size - 1 && @front != 0 # rear needs to wrap around
@rear = 0
@store[@rear] = element
else
@rear += 1
@store[@rear] = element
end
else
@rear = (@rear + 1) % @store.length
@store[@rear] = element
end

end

def dequeue
raise NotImplementedError, "Not yet implemented"
end

def front
raise NotImplementedError, "Not yet implemented"
end

def size
raise NotImplementedError, "Not yet implemented"
if @front == -1
raise RuntimeError.new("Error: The queue is empty")
end

data = @store[@front]
@store[@front] = nil # overwrite the element being deleted

if @front == @rear # queue is now empty
@front = @rear = -1
elsif @front == @size - 1 # front needs to wrap around
@front = 0
else
@front += 1
end
Comment on lines +36 to +40

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

See above for a way to simplify this.


return data
end

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

def to_s
return @store.to_s
list = Array.new

@store[@front...@size].each do |element|
list.push(element) if element
end

if @rear < @front # the queue has wrapped around
@store[0..@rear].each do |element|
list.push(element) if element
end
end

return list.to_s
end
end
13 changes: 8 additions & 5 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"