Sha256: 2c3ea65b99a182da30b65dc74e5cea7693f167833f68017987338a48fb75f67a
Contents?: true
Size: 1.22 KB
Versions: 7
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 self.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
7 entries across 7 versions & 1 rubygems