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
36 changes: 30 additions & 6 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
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"
stack = Stack.new
twosomes = {
"[" => "]",
"(" => ")",
"{" => "}"
}

string.each_char do |char|
if twosomes.values.include?(char)

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 .include? is an O(n) operation and unnecessary in a hash.

Suggested change
if twosomes.values.include?(char)
if twosomes[char]

return false if twosomes.key(char) != stack.pop
elsif twosomes.key?(char)
stack.push(char)
end
end
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"
postfix_expression.each_char do |char|
if char.match(/\d/)
stack.push(char.to_i)
else
front = stack.pop
back = stack.pop
evaluation = #{front} #{char} #{back}
stack.push(evaluation.to_i)
end
end

return stack.pop
end
52 changes: 44 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,67 @@
class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@queue = Array.new
@size = 20
@front = -1
@back = -1
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"
if @front == -1
@front = 0
@queue[@front] = element
@back = 1
elsif @front == @back
raise ArgumentError, "The queue is full; cannot enqueue at this time."
else
@queue[@back] = element
@back = (1 + @back) % @size
end
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"
if @front == -1
raise ArgumentError, "The queue is empty."
elsif @front == @back
interim = @queue[@front]
@front = -1
@back = -1

return interim
else
interim = @queue[@front]
@front = (1 + @front) % @size

return interim
end
end

def front
raise NotImplementedError, "Not yet implemented"
return @queue[@front]
end
Comment on lines 40 to +42

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You should also check to see if the Queue is empty


@private
def get_queue_contents()
if @queue.empty?
return []
elsif @front > @back
return @queue[@front...@size] + @queue[0...@back]
else
return @queue[@front...@back]
end
end
Comment on lines +45 to 53

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I like this one!


def size
raise NotImplementedError, "Not yet implemented"
self.get_queue_contents.length
end

def empty?
raise NotImplementedError, "Not yet implemented"
return self.get_queue_contents.empty?
end

def to_s
return @store.to_s
return self.get_queue_contents.to_s
end

end
15 changes: 9 additions & 6 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
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"
@stack = LinkedList.new
end

def push(element)
raise NotImplementedError, "Not yet implemented"
@stack.add_front(element)
end

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

element = stack.remove_front

return element
end

def empty?
raise NotImplementedError, "Not yet implemented"
return @stack.empty?
end

def to_s
return @store.to_s
return @stack.to_s
end
end