-
Notifications
You must be signed in to change notification settings - Fork 49
Leaves - Emily V #40
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?
Leaves - Emily V #40
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,13 +1,55 @@ | ||
require_relative './stack.rb' | ||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def balanced(string) | ||
raise NotImplementedError, "Not implemented yet" | ||
braces_stack = Stack.new | ||
string.each_char do |n| | ||
case n | ||
when "(" | ||
braces_stack.push(n) | ||
when "{" | ||
braces_stack.push(n) | ||
when "[" | ||
braces_stack.push(n) | ||
when ")" | ||
return false if !stack_checker(braces_stack, "(") | ||
when "}" | ||
return false if !stack_checker(braces_stack, "{") | ||
when "]" | ||
return false if !stack_checker(braces_stack, "[") | ||
end | ||
end | ||
return braces_stack.empty? | ||
end | ||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def stack_checker(stack, expected_paren) | ||
return !(stack.empty? || stack.pop != expected_paren) | ||
end | ||
|
||
# Time Complexity: O(n) | ||
# Space Complexity: O(n) <- but we'd probably use less assuming the expression contains operators | ||
def evaluate_postfix(postfix_expression) | ||
Comment on lines
+30
to
32
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. 👍 |
||
raise NotImplementedError, "Not implemented yet" | ||
stack = Stack.new | ||
postfix_expression.each_char do |n| | ||
if /\+|-|\*|\//.match(n) | ||
stack_operation_helper(stack, stack.pop, stack.pop, n) | ||
else | ||
stack.push(n) | ||
end | ||
end | ||
return stack.pop | ||
end | ||
|
||
def stack_operation_helper(stack, first_pop, second_pop, operator) | ||
case operator | ||
when "+" | ||
stack.push(second_pop.to_i + first_pop.to_i) | ||
when "-" | ||
stack.push(second_pop.to_i - first_pop.to_i) | ||
when "*" | ||
stack.push(second_pop.to_i * first_pop.to_i) | ||
when "/" | ||
stack.push(second_pop.to_i / first_pop.to_i) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,64 @@ | ||
class Queue | ||
|
||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
@store = Array.new(10) | ||
@front = 0 | ||
@back = 0 | ||
end | ||
|
||
def enqueue(element) | ||
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 method could use being broken into some helper methods. Otherwise it seems to work. |
||
raise NotImplementedError, "Not yet implemented" | ||
#resize if the array is full | ||
if @front == (@back + 1) % @store.length | ||
temp = Array.new(@store.length + 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. Why make it one bigger? That gets pretty expensive in terms of time. |
||
if @front > @back | ||
temp.each_index do |n| | ||
if n < @front | ||
temp[n] = @store[n] | ||
elsif n > @front | ||
temp[n] = @store[n-1] | ||
end | ||
end | ||
@front += 1 | ||
else | ||
temp.each_index do |n| | ||
temp[n] = @store[n] | ||
end | ||
end | ||
@store = temp | ||
end | ||
#add the element | ||
@store[@back] = element | ||
@back = (@back + 1) % @store.length | ||
end | ||
|
||
def dequeue | ||
raise NotImplementedError, "Not yet implemented" | ||
end | ||
|
||
if @front != @back | ||
temp = @store[@front] | ||
@store[@front] = nil | ||
@front = (@front + 1) % @store.length | ||
return temp | ||
end | ||
end | ||
|
||
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store[@front] | ||
end | ||
|
||
def size | ||
raise NotImplementedError, "Not yet implemented" | ||
return abs(@back - @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. |
||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
return @front == @back | ||
end | ||
|
||
def to_s | ||
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. 👍 |
||
return @store.to_s | ||
temp = [] | ||
curr = @front | ||
until curr == @back | ||
temp << @store[curr] if @store[curr] | ||
curr = (curr + 1) % @store.length | ||
end | ||
return "#{temp}" | ||
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.
👍