Sha256: 636ef673b2d30998108826e7e8936d6f87800ca0ce521f0fdb64867193baacfb
Contents?: true
Size: 1.25 KB
Versions: 29
Compression:
Stored size: 1.25 KB
Contents
module ProconBypassMan class CompressArray class CompressibleValue # @params [String] prev # @params [String] current def initialize(prev, current) @prev = prev @current = current end # @return [Boolean] def compress? @prev.include?(@current) end # @return [String] def to_s_with_mark if /^(.+) \* (\d+)/ =~ @prev value = $1 count = $2 return "#{value} * #{count.to_i + 1}" end if /^(.+)/ =~ @prev value = $1 return "#{value} * 1" end end end def initialize(array) @array = array end # @return [Array<String>] def compress previous_value = nil @array.reduce([]) do |acc, item| if previous_value.nil? acc << item previous_value = item next acc end if CompressibleValue.new(previous_value, item).compress? registered_value = acc.pop acc << CompressibleValue.new(registered_value, item).to_s_with_mark else acc << item end previous_value = item next acc end end end end if $0 == __FILE__ ProconBypassMan::CompressArray.new(['']) end
Version data entries
29 entries across 29 versions & 1 rubygems