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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def balanced(string)
raise NotImplementedError, "Not implemented yet"
return false if string.length % 2 != 0

struct = Stack.new
left_half = ["(", "[", "{"]
right_half = [")", "]", "}"]
Comment on lines +9 to +10

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Did you know, you could also make a hash where the keys are the open parens and the values are the close parens. That way you wouldn't have to check the indexes like you are.


left = 0
right = string.length - 1
until left >= right
index = left_half.find_index { |el| el == string[left] }

if !index.nil? && string[right] != right_half[index]
return false if string[left + 1] != right_half[index]
end

left += 1
right -= 1
end

true
end

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

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new
@size = 20
@first = 0
@last = 0
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"
return nil if @first == (@last + 1) % @size

@store[@last] = element
@last = (@last + 1) % @size
end

def dequeue

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"
return nil if self.empty?

element = @store[@first]
@first = (@first + 1) % size

return element
end

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

def size
raise NotImplementedError, "Not yet implemented"
return @size
end
Comment on lines 29 to 31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Size usually references the number of items in the Queue.


def empty?
raise NotImplementedError, "Not yet implemented"
return @first == @last
end

def to_s
return @store.to_s
return nil if self.empty?

string_queue = Array.new
current_element = @first

until current_element == @last
string_queue << @store[current_element]
current_element = (current_element + 1) % @size
end

return string_queue.to_s
end
end
12 changes: 7 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
require_relative './linked_list.rb'

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_last(element)
end

def pop
raise NotImplementedError, "Not yet implemented"
return nil if self.empty?
element = @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"