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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# 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"
return false if string.length.odd?
brackets = {"{" => "}", "[" => "]", "(" => ")"}
bracket_stack = Stack.new
# for each char, if bracket, push it into stack
string.chars.each do |char|
if brackets[char]
bracket_stack.push(brackets[char])
else # pop last item, see if it matches char, and if not return false
if bracket_stack.pop != char
return false
end
end
end
return bracket_stack.empty? # must be empty to return true
end

# Time Complexity: ?
# Space Complexity: ?
def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"
# new stack
end
43 changes: 31 additions & 12 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
class Queue

class Queue # FIFO; Elements added to the rear and removed from front
# An Abstract Data Type is a data type with a specific public interface, but unspecified implementation.
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(20)
@size = 20
@front = 0
@back = 0

end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
def enqueue(element) # add an element to the rear of the 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.

👍

if @front == (@back + 1) % size
raise ArgumentError, "Queue is full"
else # move back to next position clockwise
@store[@back] = element
@back = (@back + 1) % @size
end
end

def dequeue
raise NotImplementedError, "Not yet implemented"
def dequeue # Remove & return the element from the front

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 @front == @back
raise ArgumentError, "Queue is empty"
else # move front to next position clockwise
result = @store[@front]
@front = (@front + 1) % @size
return result
end
end

def front
raise NotImplementedError, "Not yet implemented"
return @store[@front]
end

def size

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 would be better to return the number of elements in the Queue rather than the size of the internal buffer.

raise NotImplementedError, "Not yet implemented"
return @store.size
end

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

def to_s
return @store.to_s
result = []
@store.each do |element|
result << element if element
end
return result.to_s
end

end
10 changes: 5 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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"
return nil if self.empty?
@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 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
3 changes: 1 addition & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
require_relative "../lib/linked_list.rb"
require_relative "../lib/queue.rb"
require_relative "../lib/stack.rb"
# Extra exercises
# require_relative "../lib/problems.rb"
require_relative "../lib/problems.rb"