Решение на Първа задача от Илия Ватахов

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

Към профила на Илия Ватахов

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 13 успешни тест(а)
  • 1 неуспешни тест(а)

Код

class Integer
def prime?
self > 1 and ('1' * self) !~ /^(11+?)\1+$/
end
def prime_factorization(natural_number)
return [] if natural_number == 1
prime_factor = (2..natural_number).find { |x| natural_number % x == 0 }
[prime_factor] + prime_factorization(natural_number / prime_factor)
end
def prime_factors
prime_factorization(self.abs)
end
def harmonic
(1..self).inject(0){ |sum,n| sum + Rational(1) / n }
end
def digits
self.abs.to_s.chars.map(&:to_i)
end
end
class Array
def frequencies
elements_frequency = Hash.new(0)
self.each { |element| elements_frequency[element] += 1 }
elements_frequency
end
def average
self.inject(:+).to_f / self.count
end
def drop_every(n)
remained_indexes = (1..self.count).select { |a| a % n != 0 }
remained_indexes.map { |a| self[a - 1] }
end
def combine_with(other)
self.zip(other).flatten.compact + Array(other[self.count..-1])
end
end

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

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

Failures:

  1) 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-yr6g6k/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.01927 seconds
14 examples, 1 failure

Failed examples:

rspec /tmp/d20131023-4395-yr6g6k/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Илия обнови решението на 16.10.2013 15:56 (преди около 11 години)

+class Integer
+ def prime?
+ self > 1 and ('1' * self) !~ /^(11+?)\1+$/
+ end
+
+ def prime_factorization(natural_number)
+ return [] if natural_number == 1
+ prime_factor = (2..natural_number).find { |x| natural_number % x == 0 }
+ [prime_factor] + prime_factorization(natural_number / prime_factor)
+ end
+
+ def prime_factors
+ prime_factorization(self.abs)
+ end
+
+ def harmonic
+ (1..self).inject(0){ |sum,n| sum + Rational(1) / n }
+ end
+
+ def digits
+ self.abs.to_s.chars.map(&:to_i)
+ end
+end
+
+class Array
+ def frequencies
+ elements_frequency = Hash.new(0)
+ self.each { |element| elements_frequency[element] += 1 }
+ elements_frequency
+ end
+
+ def average
+ self.inject(:+).to_f / self.count
+ end
+
+ def drop_every(n)
+ remained_indexes = (1..self.count).select { |a| a % n != 0 }
+ remained_indexes.map { |a| self[a - 1] }
+ end
+
+ def combine_with(other)
+ self.zip(other).flatten.compact + Array(other[self.count..-1])
+ end
+end