Sha256: 37956eef70a89cc4e363074ecf730bda39239f013f6c7d4366fdc56ea323dc62

Contents?: true

Size: 831 Bytes

Versions: 396

Compression:

Stored size: 831 Bytes

Contents

Palindrome = Struct.new(:value, :factors)

class Palindromes
  attr_reader :range
  def initialize(options)
    max = options.fetch(:max_factor)
    min = options.fetch(:min_factor) { 1 }
    @range = (min..max)
  end

  def generate
    @palindromes = {}
    range.each do |i|
      range.each do |j|
        product = i * j
        if palindrome?(product)
          palindrome = @palindromes[product] || Palindrome.new(product, [])
          palindrome.factors << [i, j].sort
          palindrome.factors.uniq!
          @palindromes[product] = palindrome
        end
      end
    end
  end

  def palindrome?(number)
    number.to_s == number.to_s.reverse
  end

  def sort
    @palindromes.sort_by do |key, _palindrome|
      key
    end
  end

  def largest
    sort.last[1]
  end

  def smallest
    sort.first[1]
  end
end

Version data entries

396 entries across 396 versions & 1 rubygems

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