Class: MinMaxStack
- Inherits:
-
Object
- Object
- MinMaxStack
- Defined in:
- lib/min_max_stack.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
-
#initialize ⇒ MinMaxStack
constructor
A new instance of MinMaxStack.
- #length ⇒ Object
- #max ⇒ Object
- #min ⇒ Object
- #pop ⇒ Object
- #push(val) ⇒ Object
Constructor Details
#initialize ⇒ MinMaxStack
Returns a new instance of MinMaxStack.
4 5 6 |
# File 'lib/min_max_stack.rb', line 4 def initialize @store = [] end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
2 3 4 |
# File 'lib/min_max_stack.rb', line 2 def store @store end |
Instance Method Details
#length ⇒ Object
8 9 10 |
# File 'lib/min_max_stack.rb', line 8 def length @store.length end |
#max ⇒ Object
12 13 14 |
# File 'lib/min_max_stack.rb', line 12 def max @store.empty? ? nil : @store[-1][2] end |
#min ⇒ Object
16 17 18 |
# File 'lib/min_max_stack.rb', line 16 def min @store.empty? ? nil : @store[-1][1] end |
#pop ⇒ Object
20 21 22 23 24 |
# File 'lib/min_max_stack.rb', line 20 def pop val, _ = @store.pop val end |
#push(val) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/min_max_stack.rb', line 26 def push(val) if @store.empty? @store << [val, val, val] else @store << [val, [self.min, val].min, [self.max, val].max] end end |