Skip to content

Instantly share code, notes, and snippets.

@tsnow
Created January 24, 2015 18:59
Show Gist options
  • Save tsnow/fbb209dd609e61b5930f to your computer and use it in GitHub Desktop.
Save tsnow/fbb209dd609e61b5930f to your computer and use it in GitHub Desktop.
Min stack
#How does this look?
class Stack
  class Start; end
  class Empty < null; end
  def pop
    return Empty if empty?
    .. #implementation
  end
  def push(val)
    return nil if val == Empty
    #...implementation
  end
  def each
    swap=Stack.new
    more=Start
    while more != Empty do
      more = current = self.pop
      swap.push(yield current)
    end
    more=Start
    while more != Empty do
      more = current = swap.pop
      self.push(current)
    end
  include Enumerable
end
   
   
def min_stack(stack)
  stack.min
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment