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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - length of the string
# Space Complexity: O(n) - # of opening brackets

# Given a string containing opening and closing braces, check if it represents a balanced expression or not.
def balanced(string)
raise NotImplementedError, "Not implemented yet"
stack = Stack.new
opening = ["(", "[", "{"]

string.each_char do |char|
# if character is an opening bracket
if opening.include?(char)
# push to stack
stack.push(char)
# if not
else
# pop the top character from the stack
top_stack = stack.pop
if char == ')'
return false if top_stack != '('
elsif char == ']'
return false if top_stack != '['
elsif char == '}'
return false if top_stack != '{'
# if the current character is a closing bracket
# return false if the top stack character is not a matching open
end
Comment on lines +20 to +28
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Instead of this, you could simplify it with a hash. where the key is the close brace and the matching open brace is the value.

end


end

# return true if the stack is empty, false if not
return stack.empty?
end

# Time Complexity: ?
Expand Down
58 changes: 45 additions & 13 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,63 @@
class Queue

# Using a circular buffer with an internal array starting at 20 elements, implement a Queue

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"
# Adds the value to the back of the queue.
def enqueue(value)
if @front == -1
@front = 0
@back = 1
@store[@front] = value
elsif @store.empty?
raise ArgumentError, "Queue is full"
Comment on lines +18 to +19
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note about the readability here. You raise a Queue is full error if the array is empty?

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

# removes and returns a value from the front of the queue
def dequeue
raise NotImplementedError, "Not yet implemented"
if @front == -1
raise ArgumentError, "Queue is empty"
else
data = @store[@front]
@front = (@front + 1) % @size
if @store.empty?
@front = back = -1
end
return data
end
end

def front
raise NotImplementedError, "Not yet implemented"
end
# def front
# raise NotImplementedError, "Not yet implemented"
# end

def size
raise NotImplementedError, "Not yet implemented"
end
# def size
# raise NotImplementedError, "Not yet implemented"
# end

# returns true if the queue is empty and false otherwise
def empty?
raise NotImplementedError, "Not yet implemented"
return @front == @back
end
Comment on lines 49 to 51
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 reads better if @front == -1 && @back == -1


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
17 changes: 11 additions & 6 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
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"
# Adds the value to the top of the stack
def push(value)
@store.add_last(value)
end

# Removes and returns an element from the top of the stack
def pop
raise NotImplementedError, "Not yet implemented"
return nil if @store.empty?

@store.remove_last
end

# returns true if the stack is empty and false otherwise
def empty?
raise NotImplementedError, "Not yet implemented"
return true if @store.empty?
return false # if not 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
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"