Sha256: 2300241cee6b207e73cf67802181db4aaeae53f22cbff9a94e83da699dbfad78

Contents?: true

Size: 855 Bytes

Versions: 396

Compression:

Stored size: 855 Bytes

Contents

class Triplets
  attr_reader :factors, :sum
  def initialize(conditions)
    min = conditions.fetch(:min_factor) { 1 }
    max = conditions.fetch(:max_factor)
    @sum = conditions[:sum]
    @factors = (min..max).to_a
  end

  def to_a
    triplets = []
    each_triplet do |triplet|
      triplets << triplet if select?(triplet)
    end
    triplets
  end

  def each_triplet
    factors.combination(3).each do |a, b, c|
      yield Triplet.new(a, b, c)
    end
  end

  def select?(triplet)
    triplet.pythagorean? && (!sum || triplet.sum == sum)
  end
end

class Triplet
  def self.where(conditions)
    Triplets.new(conditions).to_a
  end

  attr_reader :a, :b, :c
  def initialize(a, b, c)
    @a = a
    @b = b
    @c = c
  end

  def sum
    a + b + c
  end

  def product
    a * b * c
  end

  def pythagorean?
    a**2 + b**2 == c**2
  end
end

Version data entries

396 entries across 396 versions & 1 rubygems

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