Решение на Първа задача от Борислава Аладжова

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

Към профила на Борислава Аладжова

Резултати

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

Код

class Integer
def prime?
return false if self < 0
2.upto(self - 1).all? { |divisor| remainder(divisor).nonzero? }
end
def prime_factors
factors = []
number = self
2.upto(self.abs) do |factor|
while (number % factor).zero? and factor.prime?
factors << factor and number /= factor
end
end
factors
end
def harmonic
harmonic_number = Rational(1, 1)
2.upto(self.abs) do |number|
harmonic_number += Rational(1, number)
end
harmonic_number
end
def digits
number = self.abs.to_s
digits_list = []
number.each_char { |i| digits_list << i.to_i }
digits_list
end
end
class Array
def frequencies
result = {}
self.each do |key|
if result.has_key?(key)
result[key] += 1
else
result[key] = 1
end
end
result
end
def average
average_sum = 0.0
self.each do |number|
average_sum += number
end
average_sum / self.length
end
def drop_every(n)
result = []
self.each_with_index do |element, index|
result << element unless index % n == n - 1
end
result
end
def combine_with(other)
self.zip(other).flatten.compact
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-7njv5p/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: [].combine_with([1, 2, 3]).should                   eq [1, 2, 3]
       
       expected: [1, 2, 3]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-7njv5p/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.01964 seconds
14 examples, 2 failures

Failed examples:

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

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

Борислава обнови решението на 15.10.2013 14:32 (преди около 11 години)

+class Integer
+ def prime?
+ return false if self < 0
+
+ 2.upto(self - 1).all? { |divisor| remainder(divisor).nonzero? }
+ end
+
+ def prime_factors
+ factors = []
+ number = self
+
+ 2.upto(self.abs) do |factor|
+ while (number % factor).zero? and factor.prime?
+ factors << factor and number /= factor
+ end
+ end
+
+ factors
+ end
+
+ def harmonic
+ harmonic_number = Rational(1, 1)
+
+ 2.upto(self.abs) do |number|
+ harmonic_number += Rational(1, number)
+ end
+
+ harmonic_number
+ end
+
+ def digits
+ number = self.abs.to_s
+ digits_list = []
+
+ number.each_char { |i| digits_list << i.to_i }
+
+ digits_list
+ end
+end
+
+class Array
+ def frequencies
+ result = {}
+
+ self.each do |key|
+ if result.has_key?(key)
+ result[key] += 1
+ else
+ result[key] = 1
+ end
+ end
+
+ result
+ end
+
+ def average
+ average_sum = 0.0
+
+ self.each do |number|
+ average_sum += number
+ end
+
+ average_sum / self.length
+ end
+
+ def drop_every(n)
+ result = []
+
+ self.each_with_index do |element, index|
+ result << element unless index % n == n - 1
+ end
+
+ result
+ end
+
+ def combine_with(other)
+ self.zip(other).flatten.compact
+ end
+end