Решение на Първа задача от Петър Мазълов

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

Към профила на Петър Мазълов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 12 успешни тест(а)
  • 2 неуспешни тест(а)

Код

class Integer
def prime?
if self < 0 or self == 1
return false
else
(2..self-1).all? { |n| self % n != 0 }
end
end
def prime_factors
prime_divisors = (2..abs).select { |n| n.prime? and self % n == 0 }
multiplicity = prime_divisors.map { |n| abs.divisor_multiplicity(n) }
prime_divisors.zip(multiplicity).map { |n, count| [n] * count }.flatten
end
def divisor_multiplicity(divisor)
if self % divisor == 0
1 + (self / divisor).divisor_multiplicity(divisor)
else
0
end
end
def digits
number = abs
if number > 10
(number / 10).digits + [number % 10]
else
[number]
end
end
def harmonic
(1..self).map { |n| Rational(1, n) }.reduce(:+)
end
end
class Array
def average
sum = 0
each { |n| sum += n}
sum / self.count.to_f
end
def frequencies
reduce(Hash.new(0)) do |frequencies, element|
frequencies[element] += 1
frequencies
end
end
def drop_every(n)
result = []
(1..size).each do |i|
if i % n != 0
result << self[i - 1]
end
end
result
end
def combine_with(other)
if size < other.size
zip(other).flatten.compact + other.slice(size, other.size)
else
zip(other).flatten.compact
end
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: 0.prime?.should   eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-1ow5ea/spec.rb:4: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) 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-1ow5ea/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.05092 seconds
14 examples, 2 failures

Failed examples:

rspec /tmp/d20131023-4395-1ow5ea/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-1ow5ea/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Петър обнови решението на 15.10.2013 09:37 (преди над 10 години)

+class Integer
+ def prime?
+ if self < 0 or self == 1
+ return false
+ else
+ (2..self-1).all? { |n| self % n != 0 }
+ end
+ end
+
+ def prime_factors
+ prime_divisors = (2..abs).select { |n| n.prime? and self % n == 0 }
+ multiplicity = prime_divisors.map { |n| abs.divisor_multiplicity(n) }
+ prime_divisors.zip(multiplicity).map { |n, count| [n] * count }.flatten
+
+ end
+
+ def divisor_multiplicity(divisor)
+ if self % divisor == 0
+ 1 + (self / divisor).divisor_multiplicity(divisor)
+ else
+ 0
+ end
+ end
+
+ def digits
+ number = abs
+ if number > 10
+ (number / 10).digits + [number % 10]
+ else
+ [number]
+ end
+ end
+
+ def harmonic
+ (1..self).map { |n| Rational(1, n) }.reduce(:+)
+ end
+end
+
+
+class Array
+ def average
+ sum = 0
+ each { |n| sum += n}
+ sum / self.count.to_f
+ end
+
+ def frequencies
+ reduce(Hash.new(0)) do |frequencies, element|
+ frequencies[element] += 1
+ frequencies
+ end
+ end
+
+ def drop_every(n)
+ result = []
+ (1..size).each do |i|
+ if i % n != 0
+ result << self[i - 1]
+ end
+ end
+ result
+ end
+
+ def combine_with(other)
+ if size < other.size
+ zip(other).flatten.compact + other.slice(size, other.size)
+ else
+ zip(other).flatten.compact
+ end
+ end
+end