Created
January 24, 2015 18:59
-
-
Save tsnow/fbb209dd609e61b5930f to your computer and use it in GitHub Desktop.
Min stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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