Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIRE - Tram #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
30 changes: 26 additions & 4 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
require_relative './stack.rb'

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - has to iterate through each char in the string input
# Space Complexity: O(n) - the stack is directly correlated with the string.length
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.

👍

raise NotImplementedError, "Not implemented yet"
return true if string.empty?

parens_hash = {"{" => "}",
"[" => "]",
"(" => ")"
}

stack = Stack.new

string.each_char do |char|
# check if it's an open paren
if parens_hash.include?(char)
stack.push(parens_hash[char])
else # it's a closed paren
paren = stack.pop
return false if char != paren
end
end

return stack.empty?
end

# Time Complexity: ?
# Space Complexity: ?
def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"
stack = Stack.new



end
63 changes: 54 additions & 9 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,76 @@
class Queue

MAX_BUFFER = 20
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(MAX_BUFFER)
@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"
# check if queue if full
if @front == 0 && @rear == MAX_BUFFER - 1
raise ArgumentError, "Queue is full, sorry"
elsif @front == -1 # queue is empty
@front = @rear = 0
@store[@rear] = element
elsif (@rear == MAX_BUFFER - 1 && @front != 0 ) # rear need to wrap
@rear = 0
@store[@rear] = element
else
@rear = @rear + 1
@store[@rear] = element
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"

raise ArgumentError, "Queue is empty" if @front == -1

data = @store[@front]
# @store[@front] = nil

# if the queue is now empty
if @front == @rear
@front = @rear = -1
elsif @front + 1 == MAX_BUFFER # front needs to wrap around
@front = 0
else
@front = @front + 1
end

return data


end

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

# TODO: look at this again
def size
raise NotImplementedError, "Not yet implemented"
return @store.length
end

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

def to_s
return @store.to_s
# return @store.to_s

return "[]" if empty?
arr = []
current = @front

until current == @rear
arr << @store[cur]
current = (current + 1) % MAX_BUFFER
end

arr << @store[current]

return arr.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
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = LinkedList.new
end

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

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

return @store.remove_first
end

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

def to_s
Expand Down
2 changes: 1 addition & 1 deletion 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
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"