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
18 changes: 15 additions & 3 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
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

Choose a reason for hiding this comment

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

👍 Nice work!

raise NotImplementedError, "Not implemented yet"
stack = Stack.new
string_array = string.split('')
pair_hash = { "}" => "{", "]" => "[", ")" => "(" }

string_array.each do |char|
if !pair_hash[char]
stack.push(char)
elsif stack.pop != pair_hash[char]
return false
end
end

return stack.empty?
end

# Time Complexity: ?
Expand Down
48 changes: 37 additions & 11 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,57 @@
class Queue

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

def enqueue(element)

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
@front = 0
@rear = 1
@store[@front] = element

elsif @front == @rear
raise ArgumentError, "Queue is at max capacity"

else
@store[@rear] = element
@rear = (@rear + 1) % @max_size
end
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"
end
if @front == -1
raise ArgumentError, "Queue is empty"
else
element = @store[@front]
@front = (@front + 1) % @max_size

def front
raise NotImplementedError, "Not yet implemented"
if @front == @rear
@front = @rear = -1
end

return element
end
end

def size
raise NotImplementedError, "Not yet implemented"
def front
return @store[@front]
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 == @rear
end

def to_s
return @store.to_s
queue = []
current = @front
while current != @rear
queue << @store[current]
current = (current + 1) % @max_size
end
return queue.to_s
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 # using provided Linked List class
end

def push(element)
raise NotImplementedError, "Not yet implemented"
@store.add_last(element)
end

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