Решение на Първа задача от Николай Хубанов

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

Към профила на Николай Хубанов

Резултати

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

Код

class Integer
def prime?
self > 1 and (2..(Math.sqrt(self))).all? { |divider| self % divider != 0 }
end
def prime_factors
prime_factors = []
(2..self.abs).select { |number| number.prime? }.each do |prime_number|
value = self.abs
while value % prime_number == 0 do
value /= prime_number and prime_factors << prime_number
end
end
prime_factors
end
def harmonic
(1..self).inject { |result, number| result + Rational(1, number) }
end
def digits
self.abs.to_s.chars.map { |digit| digit.to_i }
end
end
class Array
def average
inject(:+) / length.to_f
end
def combine_with(other)
min_length = [length, other.length].min
result = zip(other).take(min_length).flatten(1)
result += drop(min_length)
result += other.drop(min_length)
end
def drop_every(n)
each_slice(n).map { |slice| slice[0...(n - 1)] }.flatten(1)
end
def frequencies
Hash.new { |hash, key| 0 }.tap do |result|
each { |item| result[item] += 1 }
end
end
end

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

..............

Finished in 0.03374 seconds
14 examples, 0 failures

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

Николай обнови решението на 16.10.2013 17:22 (преди около 11 години)

+class Integer
+ def prime?
+ self > 1 and (2..(Math.sqrt(self))).all? { |divider| self % divider != 0 }
+ end
+
+ def prime_factors
+ prime_factors = []
+ (2..self.abs).select { |divider| divider.prime? }.each do |prime_divider|
+ value = self.abs
+ while value % prime_divider == 0 do
+ value /= prime_divider and prime_factors << prime_divider
+ end
+ end
+
+ prime_factors
+ end
+
+ def harmonic
+ (1..self).inject { |result, number| result + Rational(1, number) }
+ end
+
+ def digits
+ self.abs.to_s.chars.map { |digit| digit.to_i }
+ end
+end
+
+class Array
+ def average
+ inject(:+) / length.to_f
+ end
+
+ def combine_with(other)
+ min_length = [length, other.length].min
+
+ result = zip(other).take(min_length).flatten(1)
+ result += drop(min_length)
+ result += other.drop(min_length)
+ end
+
+ def drop_every(n)
+ each_slice(n).map { |slice| slice[0...(n - 1)] }.flatten(1)
+ end
+
+ def frequencies
+ Hash.new { |hash, key| 0 }.tap do |result|
+ each { |item| result[item] += 1 }
+ end
+ end
+end

Николай обнови решението на 16.10.2013 17:29 (преди около 11 години)

class Integer
def prime?
self > 1 and (2..(Math.sqrt(self))).all? { |divider| self % divider != 0 }
end
def prime_factors
prime_factors = []
- (2..self.abs).select { |divider| divider.prime? }.each do |prime_divider|
+ (2..self.abs).select { |number| number.prime? }.each do |prime_number|
value = self.abs
- while value % prime_divider == 0 do
- value /= prime_divider and prime_factors << prime_divider
+ while value % prime_number == 0 do
+ value /= prime_number and prime_factors << prime_number
end
end
prime_factors
end
def harmonic
(1..self).inject { |result, number| result + Rational(1, number) }
end
def digits
self.abs.to_s.chars.map { |digit| digit.to_i }
end
end
class Array
def average
inject(:+) / length.to_f
end
def combine_with(other)
min_length = [length, other.length].min
result = zip(other).take(min_length).flatten(1)
result += drop(min_length)
result += other.drop(min_length)
end
def drop_every(n)
each_slice(n).map { |slice| slice[0...(n - 1)] }.flatten(1)
end
def frequencies
Hash.new { |hash, key| 0 }.tap do |result|
each { |item| result[item] += 1 }
end
end
end