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

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), will always have to traverse through the string
# Space Complexity: O(n), because in the worst case all char in string
# are opening brackets, best case would be O(1) where we push and pop every item
def balanced(string)
Comment on lines +3 to 6

Choose a reason for hiding this comment

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

👍 , However you can dry up the case statement by using a hash instead. The has would have the open braces as the keys and the closing braces as the values.

return false if brace_hash[current_char] != char

raise NotImplementedError, "Not implemented yet"
return false if string.nil?

string_stack = Stack.new

# Traversal of string
string.each_char { |char|
# push to stack if the character is an opening brace
if char == '{' or char == '[' or char == '('
string_stack.push(char)
else
# pop brace from stack and compare
# char, return false if character doesn't match
current_char = string_stack.pop()
case current_char
when '('
return false if char != ')'
when '{'
return false if char != '}'
when '['
return false if char != ']'
end
end
}

# if stack is empty, return true
# else return false since it's unbalanced
string_stack.empty? ? true : false
end

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

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
attr_accessor :ring, :front, :back, :size

def initialize(size=20)
@size = size
@ring = Array.new(size)
@front = 0
@back = 0
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 circular buffer is full, return empty array
return [] if (@back + 1) % @size == @front

# otherwise, add element to the back
@ring[@back] = element

# change @back to next free position, clockwise
@back = (@back + 1) % @size
end

def dequeue
raise NotImplementedError, "Not yet implemented"
return [] if @ring.empty?

dequeued_element = @ring[@front]
# dequeue by reassigning the front to the next position, clockwise
@front = (@front + 1) % @size

return dequeued_element
end

def front
raise NotImplementedError, "Not yet implemented"
@front

Choose a reason for hiding this comment

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

Suggested change
@front
@store[@front]

end

def size
raise NotImplementedError, "Not yet implemented"
@size
end
Comment on lines 37 to 39

Choose a reason for hiding this comment

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

Size should be the number of elements in the queue. You can calculate it using the position of front and rear.


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

def to_s
return @store.to_s
return [] if @ring.empty?

ring_output = []
current_element = @front

while current_element != @back
# only get elements that are not nil
ring_output << @ring[current_element]

# shift to next element clockwise
current_element = (current_element + 1) % @size
end

return ring_output.to_s
end
end
11 changes: 6 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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"
@list = LinkedList.new
end

def push(element)
raise NotImplementedError, "Not yet implemented"
@list.add_first(element)
end

def pop
raise NotImplementedError, "Not yet implemented"
return nil if self.empty?

@list.remove_first
end

def empty?
raise NotImplementedError, "Not yet implemented"
return @list.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
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
require_relative "../lib/linked_list.rb"
require_relative "../lib/queue.rb"
require_relative "../lib/stack.rb"
require_relative '../lib/problems.rb'
# Extra exercises
# require_relative "../lib/problems.rb"