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

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

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

Резултати

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

Код

require 'prime'
class Integer
def prime?
2.upto(Math.sqrt(abs)).each do |potential_divisor|
return false if self % potential_divisor == 0
end
true
end
def prime_factors
prime_factors = []
abs.prime_division.map { |factor, power| Array.new(power, factor) }.flatten
end
def harmonic
1.upto(self).map { |n| Rational(1, n) }.inject { |a, b| a + b }
end
def digits
abs.to_s.split(//).map { |digit| digit.to_i }
end
end
class Array
def frequencies
frequencies = Hash.new(0)
each { |item| frequencies[item] += 1 }
return frequencies
end
def average
inject { |a, b| a + b }. to_f / size
end
def drop_every(n)
each_slice(n).map { |s| if s.size == n then s[0...-1] else s end }.flatten
end
def combine_with(array)
if array.size <= size
zip(array).flatten.compact
else
zip(array).flatten.compact + array.drop(size)
end
end
end

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

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

Failures:

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

Failed examples:

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

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

Мария обнови решението на 15.10.2013 03:10 (преди над 10 години)

+require 'prime'
+
+class Integer
+ def prime?
+ 2.upto(Math.sqrt(abs)).each do |potential_divisor|
+ return false if self % potential_divisor == 0
+ end
+ true
+ end
+
+ def prime_factors
+ prime_factors = []
+ abs.prime_division.map { |factor, power| Array.new(power, factor) }.flatten
+ end
+
+ def harmonic
+ 1.upto(self).map { |n| Rational(1, n) }.inject { |a, b| a + b }
+ end
+
+ def digits
+ abs.to_s.split(//).map { |digit| digit.to_i }
+ end
+end
+
+class Array
+ def frequencies
+ frequencies = Hash.new(0)
+ each { |item| frequencies[item] += 1 }
+ return frequencies
+ end
+
+ def average
+ inject { |a, b| a + b }. to_f / size
+ end
+
+ def drop_every(n)
+ each_slice(n).map { |s| if s.size == n then s[0...-1] else s end }.flatten
+ end
+
+ def combine_with(array)
+ if array.size <= size
+ zip(array).flatten.compact
+ else
+ zip(array).flatten.compact + array.drop(size)
+ end
+ end
+end