Решение на Първа задача от Наталия Пацовска

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

Към профила на Наталия Пацовска

Резултати

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

Код

class Array
def frequencies
unique_elements = self.uniq
unique_elements_counts = unique_elements.map { |e| self.count(e) }
Hash[unique_elements.zip unique_elements_counts]
end
def sum
inject(0.0) { |result, el| result + el }
end
def average
sum / size
end
def drop_every(n)
self.each_with_index.map { |item, i| item unless (i + 1) % n == 0}.compact
end
def combine_with(other)
self.zip(other).flatten.compact
end
end
class Integer
def prime?
return false if self <= 0
(2..Math.sqrt(self)).each do |i|
if self % i == 0 && i < self
return false
end
end
true
end
def prime_factors
factors = []
numerator = self.abs
until numerator == 1
factors.push (2..numerator).find { |i| i.prime? && numerator % i == 0 }
numerator /= factors.last
end
factors
end
def harmonic
Rational (1..self).inject { |sum, n| sum + Rational(1) / n }
end
def digits
self.abs.to_s.chars.map { |char| char.to_i }
end
end

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

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-1getztj/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) 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-1getztj/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.01873 seconds
14 examples, 2 failures

Failed examples:

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

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

Наталия обнови решението на 15.10.2013 01:05 (преди около 11 години)

+class Array
+
+ def frequencies
+ unique_elements = self.uniq
+ unique_elements_counts = unique_elements.map { |e| self.count(e) }
+ Hash[unique_elements.zip unique_elements_counts]
+ end
+
+ def sum
+ inject(0.0) { |result, el| result + el }
+ end
+
+ def average
+ sum / size
+ end
+
+ def drop_every(n)
+ self.each_with_index.map { |item, i| item unless (i + 1) % n == 0}.compact
+ end
+
+ def combine_with(other)
+ self.zip(other).flatten.compact
+ end
+
+end
+
+class Integer
+
+ def prime?
+ return false if self <= 0
+
+ (2..Math.sqrt(self)).each do |i|
+ if self % i == 0 && i < self
+ return false
+ end
+ end
+ true
+ end
+
+ def prime_factors
+ factors = []
+ numerator = self.abs
+
+ until numerator == 1
+ factors.push (2..numerator).find { |i| i.prime? && numerator % i == 0 }
+ numerator /= factors.last
+ end
+ factors
+ end
+
+ def harmonic
+ Rational (1..self).inject { |sum, n| sum + Rational(1) / n }
+ end
+
+ def digits
+ self.abs.to_s.chars.map { |char| char.to_i }
+ end
+
+end