-
Notifications
You must be signed in to change notification settings - Fork 41
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
Water - Kareha #14
base: master
Are you sure you want to change the base?
Water - Kareha #14
Conversation
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.
Great work Kareha, you hit the learning goals here. Well done. I have one comment on one of the problems, otherwise very well done.
lib/problems.rb
Outdated
# Time Complexity: O(n*m) | ||
# Space Complexity: O(m), but also potentially O(1) | ||
def balanced(string) |
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.
👍 Since you're building a stack it can't be O(1)
lib/problems.rb
Outdated
stack = Stack.new | ||
|
||
string.each_char do |char| # time: O(n) | ||
if hash_table.has_value?(char) # time: O(m)? |
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.
I don't think you need to use has_value
here.
I think better something like this
matching_braces = {
"}" => "{",
")" => "(",
"]" => "[",
}
stack = Stack.new
string.each_char do |char|
if ! matching_braces[char]
stack.push(char)
elsif matching_braces[char] != stack.pop()
return false
end
end
return stack.empty()
lib/problems.rb
Outdated
# Time Complexity: O(n) | ||
# Space Complexity: O(m), but also potentially O(1) | ||
def evaluate_postfix(postfix_expression) |
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.
👍 , but the space and time complexities are O(n)
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
@store = Array.new(SIZE) | ||
@front = @back = -1 | ||
end | ||
|
||
def enqueue(element) |
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.
👍
@back += 1 | ||
end | ||
|
||
@store[@back] = element | ||
end | ||
|
||
def dequeue |
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.
👍
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store[@front] | ||
end | ||
|
||
def size | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store.select{|x| !x.nil?}.length | ||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
@store.map{|x| x.nil?}.all? | ||
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.
👍
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
@store.map{|x| x.nil?}.all? | ||
end | ||
|
||
def to_s |
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.
👍
@@ -1,19 +1,18 @@ | |||
class Stack |
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.
👍
Stacks and Queues
Thanks for doing some brain yoga. You are now submitting this assignment!
Comprehension Questions
push
- add element to stack 2.pop
- remove top element from stack 3.is_empty?
- boolean, return true if stack is empty, else return falseenqueue
- add to back of queue 2.dequeue
- remove from front of queue 3.is_empty
- boolean, return true if queue is empty, else return falseOPTIONAL JobSimulation