-
Notifications
You must be signed in to change notification settings - Fork 49
Branches - Kelsey #25
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,9 +1,45 @@ | ||||||||||||||
require_relative './stack.rb' | ||||||||||||||
|
||||||||||||||
# Time Complexity: ? | ||||||||||||||
# Space Complexity: ? | ||||||||||||||
# Time Complexity: O(n) | ||||||||||||||
# Space Complexity: O(n) | ||||||||||||||
def balanced(string) | ||||||||||||||
raise NotImplementedError, "Not implemented yet" | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
# paren pairs are at the same indexes | ||||||||||||||
open_parens = [ '(', '[', '{' ] | ||||||||||||||
close_parens = [')', ']', '}' ] | ||||||||||||||
|
||||||||||||||
# check if string length isn't even | ||||||||||||||
if string.length % 2 != 0 | ||||||||||||||
return false | ||||||||||||||
# check if string is empty | ||||||||||||||
elsif string.length == 0 | ||||||||||||||
return true | ||||||||||||||
else | ||||||||||||||
# check if string starts with a close paren or ends with an open paren (unbalanced) | ||||||||||||||
if (close_parens.include? string[0]) || (open_parens.include? string[-1]) | ||||||||||||||
return false | ||||||||||||||
else | ||||||||||||||
stack = Stack.new | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
string.each_char do |x| | ||||||||||||||
if open_parens.include? x | ||||||||||||||
stack.push(x) | ||||||||||||||
# if x is a close paren, remove last open paren in stack and check to ensure it's a match for the close paren x. | ||||||||||||||
elsif close_parens.include? x | ||||||||||||||
last_in_stack = stack.pop() | ||||||||||||||
if last_in_stack != open_parens[close_parens.index(x)] | ||||||||||||||
return false | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
if stack.empty? | ||||||||||||||
return true | ||||||||||||||
else | ||||||||||||||
return false | ||||||||||||||
end | ||||||||||||||
Comment on lines
+38
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# Time Complexity: ? | ||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,31 +1,59 @@ | ||||||
class Queue | ||||||
|
||||||
def initialize | ||||||
# @store = ... | ||||||
raise NotImplementedError, "Not yet implemented" | ||||||
@store = Array.new(30) | ||||||
@front = @back = -1 | ||||||
end | ||||||
|
||||||
def enqueue(element) | ||||||
raise NotImplementedError, "Not yet implemented" | ||||||
if @front == -1 && @back == -1 | ||||||
@front = 0 | ||||||
@back = 0 | ||||||
end | ||||||
|
||||||
if !@store[@back].nil? && @front == @back | ||||||
raise StandardError.new('queue is full.') | ||||||
end | ||||||
|
||||||
@store[@back] = element | ||||||
@back = (@back+1) % @store.length | ||||||
|
||||||
end | ||||||
|
||||||
def dequeue | ||||||
raise NotImplementedError, "Not yet implemented" | ||||||
dequeued = @store[@front] | ||||||
|
||||||
# remove dequeued item from queue | ||||||
@store[@front] = nil | ||||||
# move front to next item in queue | ||||||
@front = @front+1 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to make sure the front will wrap around the queue
Suggested change
You also need to check to see if the queue is empty! |
||||||
|
||||||
return dequeued | ||||||
end | ||||||
|
||||||
def front | ||||||
raise NotImplementedError, "Not yet implemented" | ||||||
return @store[@front] | ||||||
end | ||||||
|
||||||
def size | ||||||
raise NotImplementedError, "Not yet implemented" | ||||||
return @store.compact.length | ||||||
end | ||||||
Comment on lines
38
to
40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works, but it's not very efficient. |
||||||
|
||||||
def empty? | ||||||
raise NotImplementedError, "Not yet implemented" | ||||||
if self.size == 0 | ||||||
return true | ||||||
else | ||||||
return false | ||||||
end | ||||||
end | ||||||
|
||||||
|
||||||
# helper method for tests | ||||||
def length | ||||||
return @store.length | ||||||
end | ||||||
|
||||||
def to_s | ||||||
@store.compact! | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This compacts the buffer which removes any extra space you have in the buffer, which you might need in the Queue! Better to do something like this: return "[]" if @front == -1
current = @front
output = "["
next = (@front + 1) % @store.length
while next != @back
output += "#{@store[current]},"
current = (current + 1) % @store.length
next = (next + 1) % @store.length
end
output += "#{@store[current]}]"
return output |
||||||
return @store.to_s | ||||||
end | ||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍