Решение на Първа задача от Владимир Кузмов

Обратно към всички решения

Към профила на Владимир Кузмов

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 10 успешни тест(а)
  • 4 неуспешни тест(а)

Код

class Integer
def prime?
2.upto(pred).all? { |n| remainder(n).nonzero? }
end
def fermat_factorization
a = Math.sqrt(self).ceil
b = a*a - self
b = b + 2*a + 1 until Math.sqrt(b) % 1 == 0
return [(a - Math.sqrt(b)).to_i, (a + Math.sqrt(b)).to_i] - [1]
end
def prime_factors
factors = []
self.abs.fermat_factorization.each do |n|
if n.prime?
factors << n
else
factors = factors + n.prime_factors
end
end
return factors.sort
end
def harmonic
return nil if self <= 0
1.upto(self).inject { |sum, n| sum + Rational(1, n) }.to_r
end
def digits
self.abs.to_s.scan(/./).map { |n| n.to_i }
end
end
class Array
def frequencies
self.inject(Hash.new(0)) do |total, e|
total[e] += 1
total
end
end
def average
return nil if self.size.zero?
self.inject { |sum, i| sum + i }.to_f / size
end
def drop_every n
filtered = []
self.each_with_index { |e,i| filtered << e unless (i+1).remainder(n).zero? }
return filtered
end
def combine_with other
self.zip(other).flatten.compact
end
end

Лог от изпълнението

FFF.........F.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-gkogeu/spec.rb:3:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) Integer#prime_factors constructs an array containing the prime factors in ascending order
     Failure/Error: 360.prime_factors.should eq [2, 2, 2, 3, 3, 5]
     Timeout::Error:
       execution expired
     # /tmp/d20131023-4395-gkogeu/solution.rb:9:in `sqrt'
     # /tmp/d20131023-4395-gkogeu/solution.rb:9:in `fermat_factorization'
     # /tmp/d20131023-4395-gkogeu/solution.rb:15:in `prime_factors'
     # /tmp/d20131023-4395-gkogeu/solution.rb:19:in `block in prime_factors'
     # /tmp/d20131023-4395-gkogeu/solution.rb:15:in `each'
     # /tmp/d20131023-4395-gkogeu/solution.rb:15:in `prime_factors'
     # /tmp/d20131023-4395-gkogeu/spec.rb:19:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) Integer#prime_factors works with negative numbers
     Failure/Error: (-61).prime_factors.should  eq [61]
     Timeout::Error:
       execution expired
     # /tmp/d20131023-4395-gkogeu/solution.rb:9:in `sqrt'
     # /tmp/d20131023-4395-gkogeu/solution.rb:9:in `fermat_factorization'
     # /tmp/d20131023-4395-gkogeu/solution.rb:15:in `prime_factors'
     # /tmp/d20131023-4395-gkogeu/spec.rb:28:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) Array#combine_with combines two arrays by alternatingly taking elements
     Failure/Error: [].combine_with([1, 2, 3]).should                   eq [1, 2, 3]
       
       expected: [1, 2, 3]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-gkogeu/spec.rb:105:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 14.22 seconds
14 examples, 4 failures

Failed examples:

rspec /tmp/d20131023-4395-gkogeu/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-gkogeu/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-gkogeu/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-gkogeu/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

История (2 версии и 0 коментара)

Владимир обнови решението на 15.10.2013 02:03 (преди около 11 години)

+class Integer
+ def prime?
+ 2.upto(pred).all? { |n| remainder(n).nonzero? }
+ end
+
+ def fermat_factorization
+ a = Math.sqrt(self).ceil
+ b = a*a - self
+ until Math.sqrt(b) % 1 == 0
+ puts Math.sqrt(b)
+ b = b + 2*a + 1
+ end
+ return [(a - Math.sqrt(b)).to_i, (a + Math.sqrt(b)).to_i] - [1]
+ end
+
+ def prime_factors
+ factors = []
+ self.abs.fermat_factorization.each do |n|
+ if n.prime?
+ factors << n
+ else
+ factors = factors + n.prime_factors
+ end
+ end
+ return factors.sort
+ end
+
+ def harmonic
+ return nil if self <= 0
+ harmonic_number = Rational(1)
+ 2.upto(self) do |n|
+ harmonic_number += (Rational(1) / n)
+ end
+ return harmonic_number
+ end
+
+ def digits
+ self.abs.to_s.scan(/./).map { |n| n.to_i }
+ end
+end
+
+class Array
+ def frequencies
+ frequency = {}
+ self.each do |n|
+ frequency[n] = 1 + frequency.fetch(n, 0)
+ end
+ return frequency
+ end
+
+ def average
+ return nil if self.size.zero?
+ asd = self.inject { |sum, i| sum + i }.to_f / size
+ end
+
+ def drop_every n
+ puts n
+ filtered = []
+ self.each_with_index { |e,i| filtered << e unless (i+1).remainder(n).zero? }
+ return filtered
+ end
+
+ def combine_with other
+ combined = []
+ 0.upto([self.size, other.size].max) do |i|
+ combined << self[i] if self[i]
+ combined << other[i] if other[i]
+ end
+ return combined
+ end
+end

Владимир обнови решението на 15.10.2013 21:46 (преди около 11 години)

class Integer
def prime?
2.upto(pred).all? { |n| remainder(n).nonzero? }
end
def fermat_factorization
a = Math.sqrt(self).ceil
b = a*a - self
- until Math.sqrt(b) % 1 == 0
- puts Math.sqrt(b)
- b = b + 2*a + 1
- end
+ b = b + 2*a + 1 until Math.sqrt(b) % 1 == 0
return [(a - Math.sqrt(b)).to_i, (a + Math.sqrt(b)).to_i] - [1]
end
def prime_factors
factors = []
self.abs.fermat_factorization.each do |n|
if n.prime?
factors << n
else
factors = factors + n.prime_factors
end
end
return factors.sort
end
def harmonic
return nil if self <= 0
- harmonic_number = Rational(1)
- 2.upto(self) do |n|
- harmonic_number += (Rational(1) / n)
- end
- return harmonic_number
+ 1.upto(self).inject { |sum, n| sum + Rational(1, n) }.to_r
end
def digits
self.abs.to_s.scan(/./).map { |n| n.to_i }
end
end
class Array
def frequencies
- frequency = {}
- self.each do |n|
- frequency[n] = 1 + frequency.fetch(n, 0)
+ self.inject(Hash.new(0)) do |total, e|
+ total[e] += 1
+ total
end
- return frequency
end
def average
return nil if self.size.zero?
- asd = self.inject { |sum, i| sum + i }.to_f / size
+ self.inject { |sum, i| sum + i }.to_f / size
end
def drop_every n
- puts n
filtered = []
self.each_with_index { |e,i| filtered << e unless (i+1).remainder(n).zero? }
return filtered
end
def combine_with other
- combined = []
- 0.upto([self.size, other.size].max) do |i|
- combined << self[i] if self[i]
- combined << other[i] if other[i]
- end
- return combined
+ self.zip(other).flatten.compact
end
end