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

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

Към профила на Ангел Венчев

Резултати

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

Код

class Integer
def prime?
self > 1 and (2..self**0.5).map { |i| self % i }.none?(&:zero?)
end
def factor_count(factor)
return 0 unless abs % factor == 0
1 + (abs / factor).factor_count(factor)
end
def prime_factors
(2..abs).select(&:prime?).inject([]) do |result, i|
result += [i] * factor_count(i)
end
end
def harmonic
(1..self).map { |i| 1/i.to_r }.inject :+
end
def digits
abs.to_s.chars.map(&:to_i)
end
end
class Array
def frequences
each_with_object({}) do |element, hash|
hash[element] = count element
end
end
def average
inject(:+) / count.to_f
end
def drop_every(step)
(1..size).reject { |i| i % step == 0 }.map { |i| self[i-1] }
end
def combine_with(other_array)
return other_array if empty?
return self if other_array.empty?
[first] + other_array.combine_with(self[1..-1])
end
end

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

......FF......

Failures:

  1) Array#frequencies returns a map from distinct items to the number of times they appear
     Failure/Error: [].frequencies.should                    == {}
     NoMethodError:
       undefined method `frequencies' for []:Array
     # /tmp/d20131023-4395-5mua71/spec.rb:59: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#frequencies doesn't change the array
     Failure/Error: expect { array.frequencies }.to_not change { array }
     NoMethodError:
       undefined method `frequencies' for [1, 2, :c, 1, 1]:Array
     # /tmp/d20131023-4395-5mua71/spec.rb:68:in `block (3 levels) in <top (required)>'
     # /tmp/d20131023-4395-5mua71/spec.rb:68: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.05978 seconds
14 examples, 2 failures

Failed examples:

rspec /tmp/d20131023-4395-5mua71/spec.rb:58 # Array#frequencies returns a map from distinct items to the number of times they appear
rspec /tmp/d20131023-4395-5mua71/spec.rb:66 # Array#frequencies doesn't change the array

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

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

+class Integer
+ def prime?
+ self > 1 and (2..self**0.5).map { |i| self % i }.none?(&:zero?)
+ end
+
+ def factor_count(factor)
+ return 0 unless abs % factor == 0
+ 1 + (abs / factor).factor_count(factor)
+ end
+
+ def prime_factors
+ (2..abs).select(&:prime?).inject([]) do |result, i|
+ result += [i] * factor_count(i)
+ end
+ end
+
+ def harmonic
+ (1..self).map { |i| 1/i.to_r }.inject :+
+ end
+
+ def digits
+ abs.to_s.chars.map(&:to_i)
+ end
+end
+
+class Array
+ def frequences
+ each_with_object({}) do |element, hash|
+ hash[element] = count element
+ end
+ end
+
+ def average
+ inject(:+) / count.to_f
+ end
+
+ def drop_every(step)
+ (1..size).reject { |i| i % step == 0 }.map { |i| self[i-1] }
+ end
+
+ def combine_with(other_array)
+ return self if other_array.empty?
+ self[0...1] + other_array.combine_with(self[1..-1])
+ end
+end

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

class Integer
def prime?
self > 1 and (2..self**0.5).map { |i| self % i }.none?(&:zero?)
end
def factor_count(factor)
return 0 unless abs % factor == 0
1 + (abs / factor).factor_count(factor)
end
def prime_factors
(2..abs).select(&:prime?).inject([]) do |result, i|
result += [i] * factor_count(i)
end
end
def harmonic
(1..self).map { |i| 1/i.to_r }.inject :+
end
def digits
abs.to_s.chars.map(&:to_i)
end
end
class Array
def frequences
each_with_object({}) do |element, hash|
hash[element] = count element
end
end
def average
inject(:+) / count.to_f
end
def drop_every(step)
(1..size).reject { |i| i % step == 0 }.map { |i| self[i-1] }
end
def combine_with(other_array)
+ return other_array if empty?
return self if other_array.empty?
- self[0...1] + other_array.combine_with(self[1..-1])
+ [first] + other_array.combine_with(self[1..-1])
end
end