Sha256: edff9d4291795ce2e60ba99cbecaa1933370b5cb9780e4640d9f4ff42c471b81
Contents?: true
Size: 1.22 KB
Versions: 1
Compression:
Stored size: 1.22 KB
Contents
# A MinMaxStack is a stack that allows for Min and Max # to be found in constant time, unlike a stack or # array that generally takes linear time. module Zadt class MinMaxStack def initialize @values = [] end def self.help puts "Here are the functions for MinMaxStack:" puts "push(value)" puts "pop" puts "peek" puts "min" puts "max" puts "length" puts "show" puts "empty?" end def self.methods self.help end def help MinMaxStack.help end def methods help end def push(val) if @values.empty? @values.push([val, val, val]) else cur_min = @values.last[1] cur_max = @values.last[2] @values.push([val, [val, cur_min].min, [val, cur_max].max]) @values end end def pop @values.pop[0] end def peek @values.last[0] end def min @values.empty? ? "Empty" : @values.last[1] end def max @values.empty? ? "Empty" : @values.last[2] end def length @values.length end def show showthis = [] @values.map{|value| value[0]} end def empty? @values.empty? end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
zadt-0.1.1 | lib/zadt/AbstractDataTypes/MinMaxStack.rb |