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

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

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

Резултати

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

Код

class Integer
def prime?
if self > 0 and (2...self).find { |elem| self % elem == 0 } == nil
true
else
false
end
end
def prime_factors
return if self == 1
self < 0 ? value = -self : value = self
lpf = (2..value).find { |elem| elem.prime? and value % elem == 0 }
[lpf, (value/lpf).prime_factors].compact.flatten
end
def harmonic
return if self <= 0
sum = 0
(1..self).each { |elem| sum += elem.reciprocal }
sum
end
def reciprocal
Rational(1, self)
end
def digits
return if self == 0
self < 0 ? value = -self : value = self
[(value/10).digits, value % 10].flatten.compact
end
end
class Array
def frequencies
result = {}
self.each { |x| result.key?(x) ? result[x] += 1 : result[x] = 1 }
result
end
def average
sum = 0.0
self.each { |x| sum += x }
sum/self.size
end
def drop_every(n)
result = []
(1..self.size).each { |i| result << self[i-1] if i % n != 0 }
result
end
def combine_with(other)
result = []
other.size < self.size ? size = self.size : size = other.size
(0...size).each { |idx| result << self[idx] << other[idx] }
result.compact
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: 1.prime?.should   eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-s2oqr6/spec.rb:5: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#digits constructs an array containing the digits of a number
     Failure/Error: 0.digits.should      eq [0]
       
       expected: [0]
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-s2oqr6/spec.rb:44: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) 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-s2oqr6/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, 3 failures

Failed examples:

rspec /tmp/d20131023-4395-s2oqr6/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-s2oqr6/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-s2oqr6/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Милен обнови решението на 14.10.2013 13:19 (преди около 11 години)

+class Integer
+ def prime?
+ if self > 0 and (2...self).find { |elem| self % elem == 0 } == nil
+ true
+ else
+ false
+ end
+ end
+
+ def prime_factors
+ return if self == 1
+ self < 0 ? value = -self : value = self
+ lpf = (2..value).find { |elem| elem.prime? and value % elem == 0 }
+ [lpf, (value/lpf).prime_factors].compact.flatten
+ end
+
+ def harmonic
+ return if self <= 0
+ sum = 0
+ (1..self).each { |elem| sum += elem.reciprocal }
+ sum
+ end
+
+ def reciprocal
+ Rational(1, self)
+ end
+
+ def digits
+ return if self == 0
+ self < 0 ? value = -self : value = self
+ [(value/10).digits, value % 10].flatten.compact
+ end
+end
+
+class Array
+ def frequencies
+ result = {}
+ self.each { |x| result.key?(x) ? result[x] += 1 : result[x] = 1 }
+ result
+ end
+
+ def average
+ sum = 0.0
+ self.each { |x| sum += x }
+ sum/self.size
+ end
+
+ def drop_every(n)
+ result = []
+ (1..self.size).each { |i| result << self[i-1] if i % n != 0 }
+ result
+ end
+
+ def combine_with(other)
+ result = []
+ other.size < self.size ? size = self.size : size = other.size
+ (0...size).each { |idx| result << self[idx] << other[idx] }
+ result.compact
+ end
+end