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

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

Към профила на Александър Тахчиев

Резултати

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

Код

class Integer
def prime?
return false if self <= 1
limit = Math.sqrt(self).floor
2.upto(limit ).none? { |i| self % i == 0 }
end
def harmonic
(1..self).each.reduce(Rational(0)) do |harm_sum, i|
harm_sum += Rational(1, i)
end
end
def digits
abs.to_s.split("").map{|i| i.to_i}
end
def prime_factors
slf = self
divisors = (2..self.abs).select {|i| self % i == 0 and i.prime?}
divisors.each do |i| while slf % (i * i) == 0
slf = slf / i
divisors.push(i)
end
end.sort
end
end
class Array
def frequencies
reduce({}){|hash, i| hash.merge({i => count(i)}) }
end
def average
reduce(0){|sum, i| sum + i}/ length.to_f
end
def drop_every(n)
each_index.select {|i| not ((i+1) % n == 0)}.reduce([])do |arr, i|
arr.push(self[i])
end
end
def combine_with(other)
len_difr = -(other.length - self.length).abs
if self.length < other.length
then other[0..len_difr-1].zip(self).flatten + other[len_difr..-1]
elsif self.length == other.length
self.zip(other).flatten
else self[0..len_difr -1].zip(other).flatten + self[len_difr..-1]
end
end
end

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

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

Failures:

  1) Array#combine_with combines two arrays by alternatingly taking elements
     Failure/Error: [1, 2, 3].combine_with([:a, :b, :c, :d, :e]).should eq [1, :a, 2, :b, 3, :c, :d, :e]
       
       expected: [1, :a, 2, :b, 3, :c, :d, :e]
            got: [:a, 1, :b, 2, :c, 3, :d, :e]
       
       (compared using ==)
     # /tmp/d20131023-4395-1tvl8r0/spec.rb:108: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.0222 seconds
14 examples, 1 failure

Failed examples:

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

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

Александър обнови решението на 14.10.2013 09:13 (преди над 10 години)

+class Integer
+ def prime?
+ return false if self <= 1
+ limit = Math.sqrt(self).floor
+ 2.upto(limit ).none? { |i| self % i == 0 }
+ end
+
+ def harmonic
+ (1..self).each.reduce(Rational(0)) do |harm_sum, i|
+ harm_sum += Rational(1, i)
+ end
+ end
+
+ def digits
+ to_s.split("").map{|i| i.to_i}
+ end
+
+ def prime_factors
+ slf = self
+ divisors = (2..self.abs).select {|i| self % i == 0 and i.prime?}
+ divisors.each do |i|
+ while slf % (i * i) == 0
+ slf = slf / i
+ divisors.push(i)
+ end
+ end
+ end
+end
+
+class Array
+ def frequencies
+ reduce({}){|hash, i| hash.merge({i => count(i)}) }
+ end
+ def average
+ reduce(0){|sum, i| sum + i}/ length.to_f
+ end
+ def drop_every(n)
+ each_index.select {|i| not ((i+1) % n == 0)}.reduce([])do |arr, i|
+ arr.push(self[i])
+ end
+ end
+ def combine_with(other)
+ len_difr = -(other.length - self.length).abs
+ if self.length < other.length
+ then other[0..len_difr-1].zip(self).flatten + other[len_difr..-1]
+ elsif self.length == other.length
+ self.zip(other).flatten
+ else self[0..len_difr -1].zip(other).flatten + self[len_difr..-1]
+ end
+ end
+end

Александър обнови решението на 14.10.2013 09:20 (преди над 10 години)

class Integer
def prime?
return false if self <= 1
limit = Math.sqrt(self).floor
2.upto(limit ).none? { |i| self % i == 0 }
end
def harmonic
(1..self).each.reduce(Rational(0)) do |harm_sum, i|
harm_sum += Rational(1, i)
end
end
def digits
to_s.split("").map{|i| i.to_i}
end
def prime_factors
slf = self
divisors = (2..self.abs).select {|i| self % i == 0 and i.prime?}
- divisors.each do |i|
- while slf % (i * i) == 0
+ divisors.each do |i| while slf % (i * i) == 0
slf = slf / i
divisors.push(i)
end
- end
+ end.sort
end
end
class Array
def frequencies
reduce({}){|hash, i| hash.merge({i => count(i)}) }
end
def average
reduce(0){|sum, i| sum + i}/ length.to_f
end
def drop_every(n)
each_index.select {|i| not ((i+1) % n == 0)}.reduce([])do |arr, i|
arr.push(self[i])
end
end
def combine_with(other)
len_difr = -(other.length - self.length).abs
if self.length < other.length
then other[0..len_difr-1].zip(self).flatten + other[len_difr..-1]
elsif self.length == other.length
self.zip(other).flatten
else self[0..len_difr -1].zip(other).flatten + self[len_difr..-1]
end
end
-end
+end

Александър обнови решението на 14.10.2013 09:23 (преди над 10 години)

class Integer
def prime?
return false if self <= 1
limit = Math.sqrt(self).floor
2.upto(limit ).none? { |i| self % i == 0 }
end
def harmonic
(1..self).each.reduce(Rational(0)) do |harm_sum, i|
harm_sum += Rational(1, i)
end
end
def digits
- to_s.split("").map{|i| i.to_i}
+ abs.to_s.split("").map{|i| i.to_i}
end
def prime_factors
slf = self
divisors = (2..self.abs).select {|i| self % i == 0 and i.prime?}
divisors.each do |i| while slf % (i * i) == 0
slf = slf / i
divisors.push(i)
end
end.sort
end
end
class Array
def frequencies
reduce({}){|hash, i| hash.merge({i => count(i)}) }
end
def average
reduce(0){|sum, i| sum + i}/ length.to_f
end
def drop_every(n)
each_index.select {|i| not ((i+1) % n == 0)}.reduce([])do |arr, i|
arr.push(self[i])
end
end
def combine_with(other)
len_difr = -(other.length - self.length).abs
if self.length < other.length
then other[0..len_difr-1].zip(self).flatten + other[len_difr..-1]
elsif self.length == other.length
self.zip(other).flatten
else self[0..len_difr -1].zip(other).flatten + self[len_difr..-1]
end
end
-end
+end

Александър обнови решението на 15.10.2013 21:43 (преди над 10 години)

class Integer
def prime?
return false if self <= 1
limit = Math.sqrt(self).floor
2.upto(limit ).none? { |i| self % i == 0 }
end
def harmonic
(1..self).each.reduce(Rational(0)) do |harm_sum, i|
harm_sum += Rational(1, i)
end
end
def digits
abs.to_s.split("").map{|i| i.to_i}
end
def prime_factors
slf = self
divisors = (2..self.abs).select {|i| self % i == 0 and i.prime?}
divisors.each do |i| while slf % (i * i) == 0
slf = slf / i
divisors.push(i)
end
end.sort
end
end
+
class Array
def frequencies
reduce({}){|hash, i| hash.merge({i => count(i)}) }
end
+
def average
reduce(0){|sum, i| sum + i}/ length.to_f
end
+
def drop_every(n)
each_index.select {|i| not ((i+1) % n == 0)}.reduce([])do |arr, i|
arr.push(self[i])
end
end
+
def combine_with(other)
len_difr = -(other.length - self.length).abs
if self.length < other.length
then other[0..len_difr-1].zip(self).flatten + other[len_difr..-1]
elsif self.length == other.length
self.zip(other).flatten
else self[0..len_difr -1].zip(other).flatten + self[len_difr..-1]
end
end
end