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

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

Към профила на Николай Генов

Резултати

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

Код

class Integer
def prime?
self > 1 and (2..Math.sqrt(self)).all? { |div| remainder(div).nonzero? }
end
def prime_factors
factor = (2...abs).find { |div| remainder(div).zero? }
factor.nil? ? [abs] : [factor] + (abs / factor).prime_factors
end
def harmonic
(1..self).reduce { |sum,number| sum + Rational(1,number) }.to_r
end
def digits
abs.to_s.chars.map { |number| number.to_i }
end
end
class Array
def frequencies
reduce(Hash.new(0)) { |value,key| value if value[key] += 1 }
end
def average
reduce { |sum,number| sum + number } / count.to_f
end
def drop_every(n)
select.each_with_index { |_,i| (i + 1).remainder(n).nonzero? }
end
#OPTIMIZE
def combine_with(other)
list = []
until count.zero? and other.count.zero? do
list << shift unless empty?
list << other.shift unless other.empty?
end
list
end
end

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

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

Failures:

  1) Array#combine_with doesn't change the array
     Failure/Error: expect { array.combine_with [1, 2, 3] }.to_not change { array }
       result should not have changed, but did change from [:a, :b, :c] to []
     # /tmp/d20131023-4395-mcb0zh/spec.rb:115: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.01843 seconds
14 examples, 1 failure

Failed examples:

rspec /tmp/d20131023-4395-mcb0zh/spec.rb:113 # Array#combine_with doesn't change the array

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

Николай обнови решението на 13.10.2013 23:37 (преди около 11 години)

+class Integer
+ def prime?
+ self > 1 and (2..Math.sqrt(self)).none? { |div| remainder(div) == 0 }
+ end
+
+ def prime_factors
+ value = (2...abs).find { |div| abs.remainder(div) == 0 }
+ value ? [value] + (abs / value).prime_factors : [abs]
+ end
+
+ def harmonic
+ (1..self).reduce { |sum,number| sum + Rational(1,number) }.to_r
+ end
+
+ def digits
+ abs.to_s.chars.map { |number| number.to_i }
+ end
+end
+
+class Array
+ def frequencies
+ reduce(Hash.new(0)) { |hash,key| hash if hash[key] += 1 }
+ end
+
+ def average
+ reduce { |sum,number| sum + number } / count.to_f
+ end
+
+ def drop_every(n)
+ select.each_with_index { |val,index| (index + 1).remainder(n) != 0 }
+ end
+
+ #OPTIMIZE
+ def combine_with(other)
+ list = []
+
+ until count == 0 and other.count == 0 do
+ list << shift unless empty?
+ list << other.shift unless other.empty?
+ end
+
+ list
+ end
+end

Николай обнови решението на 14.10.2013 14:46 (преди около 11 години)

class Integer
def prime?
- self > 1 and (2..Math.sqrt(self)).none? { |div| remainder(div) == 0 }
+ self > 1 and (2..Math.sqrt(self)).all? { |div| remainder(div).nonzero? }
end
def prime_factors
- value = (2...abs).find { |div| abs.remainder(div) == 0 }
+ value = (2...abs).find { |div| remainder(div).zero? }
value ? [value] + (abs / value).prime_factors : [abs]
end
def harmonic
(1..self).reduce { |sum,number| sum + Rational(1,number) }.to_r
end
def digits
abs.to_s.chars.map { |number| number.to_i }
end
end
class Array
def frequencies
reduce(Hash.new(0)) { |hash,key| hash if hash[key] += 1 }
end
def average
reduce { |sum,number| sum + number } / count.to_f
end
def drop_every(n)
- select.each_with_index { |val,index| (index + 1).remainder(n) != 0 }
+ select.each_with_index { |val,index| (index + 1).remainder(n).nonzero? }
end
#OPTIMIZE
def combine_with(other)
list = []
- until count == 0 and other.count == 0 do
+ until count.zero? and other.count.zero? do
list << shift unless empty?
list << other.shift unless other.empty?
end
list
end
-end
+end

Николай обнови решението на 14.10.2013 23:00 (преди около 11 години)

class Integer
def prime?
self > 1 and (2..Math.sqrt(self)).all? { |div| remainder(div).nonzero? }
end
def prime_factors
value = (2...abs).find { |div| remainder(div).zero? }
value ? [value] + (abs / value).prime_factors : [abs]
end
def harmonic
(1..self).reduce { |sum,number| sum + Rational(1,number) }.to_r
end
def digits
abs.to_s.chars.map { |number| number.to_i }
end
end
class Array
def frequencies
- reduce(Hash.new(0)) { |hash,key| hash if hash[key] += 1 }
+ reduce(Hash.new(0)) { |value,key| value if value[key] += 1 }
end
def average
reduce { |sum,number| sum + number } / count.to_f
end
def drop_every(n)
- select.each_with_index { |val,index| (index + 1).remainder(n).nonzero? }
+ select.each_with_index { |_,i| (i + 1).remainder(n).nonzero? }
end
#OPTIMIZE
def combine_with(other)
list = []
until count.zero? and other.count.zero? do
list << shift unless empty?
list << other.shift unless other.empty?
end
list
end
end

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

class Integer
def prime?
self > 1 and (2..Math.sqrt(self)).all? { |div| remainder(div).nonzero? }
end
def prime_factors
- value = (2...abs).find { |div| remainder(div).zero? }
- value ? [value] + (abs / value).prime_factors : [abs]
+ factor = (2...abs).find { |div| remainder(div).zero? }
+ factor ? [factor] + (abs / factor).prime_factors : [abs]
end
def harmonic
(1..self).reduce { |sum,number| sum + Rational(1,number) }.to_r
end
def digits
abs.to_s.chars.map { |number| number.to_i }
end
end
class Array
def frequencies
reduce(Hash.new(0)) { |value,key| value if value[key] += 1 }
end
def average
reduce { |sum,number| sum + number } / count.to_f
end
def drop_every(n)
select.each_with_index { |_,i| (i + 1).remainder(n).nonzero? }
end
#OPTIMIZE
def combine_with(other)
list = []
until count.zero? and other.count.zero? do
list << shift unless empty?
list << other.shift unless other.empty?
end
list
end
end

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

class Integer
def prime?
self > 1 and (2..Math.sqrt(self)).all? { |div| remainder(div).nonzero? }
end
def prime_factors
factor = (2...abs).find { |div| remainder(div).zero? }
- factor ? [factor] + (abs / factor).prime_factors : [abs]
+ factor.nil? ? [abs] : [factor] + (abs / factor).prime_factors
end
def harmonic
(1..self).reduce { |sum,number| sum + Rational(1,number) }.to_r
end
def digits
abs.to_s.chars.map { |number| number.to_i }
end
end
class Array
def frequencies
reduce(Hash.new(0)) { |value,key| value if value[key] += 1 }
end
def average
reduce { |sum,number| sum + number } / count.to_f
end
def drop_every(n)
select.each_with_index { |_,i| (i + 1).remainder(n).nonzero? }
end
#OPTIMIZE
def combine_with(other)
list = []
until count.zero? and other.count.zero? do
list << shift unless empty?
list << other.shift unless other.empty?
end
list
end
end