Skip to content

Water - Kareha#14

Open
agesak wants to merge 12 commits intoAda-C14:masterfrom
agesak:master
Open

Water - Kareha#14
agesak wants to merge 12 commits intoAda-C14:masterfrom
agesak:master

Conversation

@agesak
Copy link

@agesak agesak commented Apr 13, 2021

Stacks and Queues

Thanks for doing some brain yoga. You are now submitting this assignment!

Comprehension Questions

Question Answer
What is an ADT? An ADT is a data type which does not depend on a given implementation so long as all expected methods are fulfilled.
Describe a Stack A stack is a LIFO ADT where items added to the top of the stack will be removed first. Adding and removing from a stack can be done in O(1) time, however accessing and searching are O(n).
What are the 5 methods in Stack and what does each do? 1. push - add element to stack 2. pop - remove top element from stack 3. is_empty? - boolean, return true if stack is empty, else return false
Describe a Queue A queue is a FIFO ADT where the first item added to the front of the queue will be the first one removed, and all other items are added to the back. It has O(1) time for insertion, but depending on its implementation, a queue can have a O(1) (linked list implementation/array with circular buffer) or O(n) (array implementation without circular buffer) time complexity for deletion. Accessing and searching are done in O(n) time.
What are the 5 methods in Queue and what does each do? 1. enqueue - add to back of queue 2. dequeue - remove from front of queue 3. is_empty - boolean, return true if queue is empty, else return false
What is the difference between implementing something and using something? There are a variety of ways something can be implemented and with encapsulation, these various implementations shouldn't affect how that object is used.

OPTIONAL JobSimulation

Question Answer
Did you include a sample run of your code as a comment?

Copy link

@CheezItMan CheezItMan left a 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.

Comment on lines +3 to 5
# Time Complexity: O(n*m)
# Space Complexity: O(m), but also potentially O(1)
def balanced(string)

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)?

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()

Comment on lines +22 to 24
# Time Complexity: O(n)
# Space Complexity: O(m), but also potentially O(1)
def evaluate_postfix(postfix_expression)

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)

@front = @back = -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.

👍

@store[@back] = element
end

def dequeue

Choose a reason for hiding this comment

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

👍

Comment on lines 40 to 50
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

Choose a reason for hiding this comment

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

👍

@store.map{|x| x.nil?}.all?
end

def to_s

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

Choose a reason for hiding this comment

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

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants