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

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

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

Резултати

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

Код

class Integer
def prime?
2.upto Math.sqrt(self).to_i do |div|
return false if (self % div).zero?
end
return true
end
def prime_factors
surplus, result = self, []
while surplus > 0
div, surplus = surplus.first_prime_factor
result << div
end
result
end
def harmonic
raise ArgumentError.new('Expecting a natural number') unless natural?
(1..self).inject(0.to_r) do |sum, n|
sum += Rational(1,n)
end
end
def digits
self.to_s.chars.map(&:to_i)
end
protected
def first_prime_factor
2.upto Math.sqrt(self).to_i do |div|
next unless div.prime?
return div, (self / div) if (self % div).zero?
end
return self, 0
end
def natural?
self >= 0
end
end
class Array
def frequencies
result = Hash.new
self.uniq.each do |el|
result[el] = self.count(el)
end
result
end
def average
raise ArgumentError.new("Expecting a non-empty array") if size.zero?
sum = self.inject(0.0){|sum, n| sum + n}
sum / size
end
def drop_every(n)
result = []
self.each_with_index do |el, i|
result << el unless ((i + 1) % n).zero?
end
result
end
def combine_with(other)
result = []
max_size = [self.size, other.size].max
0.upto(max_size) do |i|
result << self[i] if self[i]
result << other[i] if other[i]
end
result
end
end

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

F.F..F......F.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
     Math::DomainError:
       Numerical argument is out of domain - "sqrt"
     # /tmp/d20131023-4395-6jqimt/solution.rb:4:in `sqrt'
     # /tmp/d20131023-4395-6jqimt/solution.rb:4:in `prime?'
     # /tmp/d20131023-4395-6jqimt/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 works with negative numbers
     Failure/Error: (-4).prime_factors.should   eq [2, 2]
       
       expected: [2, 2]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-6jqimt/spec.rb:27: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#digits works with negative numbers
     Failure/Error: (-33).digits.should     eq [3, 3]
       
       expected: [3, 3]
            got: [0, 3, 3]
       
       (compared using ==)
     # /tmp/d20131023-4395-6jqimt/spec.rb:51: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: [:a, :b, :c].combine_with([1, nil, 3]).should       eq [:a, 1, :b, nil, :c, 3]
       
       expected: [:a, 1, :b, nil, :c, 3]
            got: [:a, 1, :b, :c, 3]
       
       (compared using ==)
     # /tmp/d20131023-4395-6jqimt/spec.rb:110: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 0.01834 seconds
14 examples, 4 failures

Failed examples:

rspec /tmp/d20131023-4395-6jqimt/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-6jqimt/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-6jqimt/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-6jqimt/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

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

+class Integer
+
+ def prime?
+ 2.upto Math.sqrt(self).to_i do |div|
+ return false if (self % div).zero?
+ end
+
+ return true
+ end
+
+ def prime_factors
+ surplus, result = self, []
+
+ while surplus > 0
+ div, surplus = surplus.first_prime_factor
+
+ result << div
+ end
+
+ result
+ end
+
+ def harmonic
+ raise ArgumentError.new('Expecting a natural number') unless natural?
+
+ (1..self).inject(0.to_r) do |sum, n|
+ sum += Rational(1,n)
+ end
+ end
+
+ def digits
+ self.to_s.chars.map(&:to_i)
+ end
+
+ protected
+
+ def first_prime_factor
+ 2.upto Math.sqrt(self).to_i do |div|
+ next unless div.prime?
+
+ return div, (self / div) if (self % div).zero?
+ end
+
+ return self, 0
+ end
+
+ def natural?
+ self >= 0
+ end
+
+end
+
+class Array
+
+ def frequencies
+ result = Hash.new
+
+ self.uniq.each do |el|
+ result[el] = self.count(el)
+ end
+
+ result
+ end
+
+ def average
+ raise ArgumentError.new("Expecting a non-empty array") if size.zero?
+
+ sum = self.inject(0.0){|sum, n| sum + n}
+
+ sum / size
+ end
+
+ def drop_every(n)
+ result = []
+
+ self.each_with_index do |el, i|
+ result << el unless ((i + 1) % n).zero?
+ end
+
+ result
+ end
+
+ def combine_with(other)
+ result = []
+ max_size = [self.size, other.size].max
+
+ 0.upto(max_size) do |i|
+ result << self[i] if self[i]
+ result << other[i] if other[i]
+ end
+
+ result
+ end
+
+end