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

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

Към профила на Илия Тобов

Резултати

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

Код

class Integer
def prime?
return false if self <= 1
divisors = 2.upto Math.sqrt abs
divisors.each { |divisor| if abs % divisor == 0 then return false end }
true
end
def prime_factors
return [] if abs == 1
factor = (2..abs).find { |divisor| abs % divisor == 0 and divisor.prime? }
[factor] + (abs/factor).prime_factors
end
def harmonic
sum = 0.0r
(1..abs).each { |number| sum += Rational(1, number) }
sum
end
def digits
digits = []
abs.to_s.split('').each { |digit| digits << digit.to_i }
digits
end
end
class Array
def frequencies
frequencies = Hash.new(0)
each { |key| frequencies[key] += 1 }
frequencies
end
def average
reduce { |accumulator, element| accumulator + element } / size.to_f
end
def drop_every(n)
each_slice(n).map { |list| list[0, n-1] }.flatten
end
def combine_with(other)
zip(other).flatten.compact
end
end

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

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

Failures:

  1) 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-1im6kvt/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 0.01959 seconds
14 examples, 1 failure

Failed examples:

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

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

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

+class Integer
+ def prime?
+ return false if self <= 1
+
+ divisors = 2.upto Math.sqrt abs
+ divisors.each { |divisor| if abs % divisor == 0 then return false end }
+ true
+ end
+
+ def prime_factors
+ return [] if abs == 1
+
+ factor = (2..abs).find { |divisor| abs % divisor == 0 and divisor.prime? }
+ [factor] + (abs/factor).prime_factors
+ end
+
+ def harmonic
+ sum = 0.0r
+ (1..abs).each { |number| sum += Rational(1, number) }
+ sum
+ end
+
+ def digits
+ digits = []
+ abs.to_s.split('').each { |digit| digits << digit.to_i }
+ digits
+ end
+end
+
+class Array
+ def frequencies
+ frequencies = Hash.new(0)
+ each { |key| frequencies[key] += 1 }
+ frequencies
+ end
+
+ def average
+ reduce { |accumulator, element| accumulator + element } / size.to_f
+ end
+
+ def drop_every(n)
+ dropped_every_nth = each_slice(n).map { |list| list[0, n-1] }
+ dropped_every_nth.reduce { |accumulator, element| accumulator + element }
+ end
+
+ def combine_with(other)
+ zip(other).flatten.compact
+ end
+end

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

class Integer
def prime?
return false if self <= 1
divisors = 2.upto Math.sqrt abs
divisors.each { |divisor| if abs % divisor == 0 then return false end }
true
end
def prime_factors
return [] if abs == 1
factor = (2..abs).find { |divisor| abs % divisor == 0 and divisor.prime? }
[factor] + (abs/factor).prime_factors
end
def harmonic
sum = 0.0r
(1..abs).each { |number| sum += Rational(1, number) }
sum
end
def digits
digits = []
abs.to_s.split('').each { |digit| digits << digit.to_i }
digits
end
end
class Array
def frequencies
frequencies = Hash.new(0)
each { |key| frequencies[key] += 1 }
frequencies
end
def average
reduce { |accumulator, element| accumulator + element } / size.to_f
end
def drop_every(n)
- dropped_every_nth = each_slice(n).map { |list| list[0, n-1] }
- dropped_every_nth.reduce { |accumulator, element| accumulator + element }
+ each_slice(n).map { |list| list[0, n-1] }.flatten
end
def combine_with(other)
zip(other).flatten.compact
end
end