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

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

Към профила на Радослав Даскалов

Резултати

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

Код

class Integer
def prime?
self >= 2 and 2.upto(Math.sqrt(self).to_i).all? {|d| self % d != 0}
end
def prime_factors
x, d, a = self.abs, 2, []
(x % d == 0) ? (a << d and x /= d) : (d += 1) while d <= x
return a
end
def harmonic
1.upto(self).inject(Rational(0)) {|sum, x| sum + Rational(1, x)}
end
def digits
self.abs.to_s.each_char.collect {|c| c.to_i}
end
end
class Array
def frequencies
self.each_with_object({}) {|e, h| h[e] = h.fetch(e, 0) + 1}
end
def average
self.reduce(:+).to_f / self.length
end
def drop_every(n)
self.select.with_index {|e, i| (i + 1) % n != 0}
end
def combine_with(other)
self.zip(other).flat_map {|e| e}.compact
end
end

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

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

Failures:

  1) 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-kfem6s/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.01886 seconds
14 examples, 1 failure

Failed examples:

rspec /tmp/d20131023-4395-kfem6s/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Радослав обнови решението на 14.10.2013 17:19 (преди около 11 години)

+
+class Integer
+
+ def prime?
+ if self < 2
+ false
+ else
+ 2.upto(Math.sqrt(self).to_i).all? {|d| self.remainder(d).nonzero?}
+ end
+ end
+
+ def prime_factors
+ x, d, a = self.abs, 2, []
+ x.remainder(d).zero? ? (a << d and x /= d) : d += 1 while d <= x
+ return a
+ end
+
+ def harmonic
+ 1.upto(self).inject(Rational(0)) {|sum, x| sum + Rational(1, x)}
+ end
+
+ def digits
+ self.to_s.each_char.collect {|c| c.to_i}
+ end
+
+end
+
+class Array
+
+ def frequencies
+ g = self.group_by {|e| e}
+ g.merge!(g) {|k, v| v.length}
+ end
+
+ def average
+ self.reduce(:+).to_f / self.length
+ end
+
+ def drop_every(n)
+ a = Array.new
+ self.each_with_index {|x, i| a << x if (i + 1) % n != 0}
+ return a
+ end
+
+ def combine_with(other)
+ self.zip(other).flat_map {|e| e}.compact
+ end
+
+end

Радослав обнови решението на 15.10.2013 13:41 (преди около 11 години)

-
class Integer
def prime?
- if self < 2
- false
- else
- 2.upto(Math.sqrt(self).to_i).all? {|d| self.remainder(d).nonzero?}
- end
+ self >= 2 and 2.upto(Math.sqrt(self).to_i).all? {|d| self % d != 0}
end
def prime_factors
x, d, a = self.abs, 2, []
- x.remainder(d).zero? ? (a << d and x /= d) : d += 1 while d <= x
+ (x % d == 0) ? (a << d and x /= d) : (d += 1) while d <= x
return a
end
def harmonic
1.upto(self).inject(Rational(0)) {|sum, x| sum + Rational(1, x)}
end
def digits
- self.to_s.each_char.collect {|c| c.to_i}
+ self.abs.to_s.each_char.collect {|c| c.to_i}
end
end
class Array
def frequencies
- g = self.group_by {|e| e}
- g.merge!(g) {|k, v| v.length}
+ self.each_with_object({}) {|e, h| h[e] = h.fetch(e, 0) + 1}
end
def average
self.reduce(:+).to_f / self.length
end
def drop_every(n)
- a = Array.new
- self.each_with_index {|x, i| a << x if (i + 1) % n != 0}
- return a
+ self.select.with_index {|e, i| (i + 1) % n != 0}
end
def combine_with(other)
self.zip(other).flat_map {|e| e}.compact
end
-end
+end