Sha256: 269652fb64bdf9f0840a63400ee2833acf693ef94ed05dd2880db325bf36171c

Contents?: true

Size: 1.09 KB

Versions: 239

Compression:

Stored size: 1.09 KB

Contents

# see also https://gist.github.com/blairand/5237976
# see also https://gist.github.com/burtlo/89b0b817fdccf6bdf20f
module BookKeeping
  VERSION = 3
end

class Series
  attr_reader :digits
  def initialize(numeric_string)
    @digits = numeric_string
  end

  def largest_product(length)
    @length = length
    validate_length
    return 1 if @digits.empty?
    collection_of_digits
    select_max { reduce_to_product  { validate  { separate } } }
  end

  private

  def validate_length
    @length < 0 and
      fail(ArgumentError.new 'Length must be non-negative')
    @length > digits.length and
      fail(ArgumentError.new 'Not enough digits')
  end

  def validate
    yield.take_while { |array| array.size == @length }
  end

  def reduce_to_product
    yield.map { |array| array.inject(1, :*) }
  end

  def select_max
    yield.max
  end

  def separate
    digits.map.with_index do |_, index|
      digits[index, @length]
    end
  end

  def collection_of_digits
    @digits !~ /^\d*$/ and
      fail(ArgumentError.new 'String must contain only digits')
    @digits = digits.chars.map(&:to_i)
  end
end

Version data entries

239 entries across 239 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.179 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.178 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.177 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.176 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.175 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.174 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.173 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.172 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.171 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.170 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.169 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.167 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.166 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.165 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.164 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.163 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.162 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.161 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb
trackler-2.2.1.160 tracks/ruby/exercises/largest-series-product/.meta/solutions/largest_series_product.rb