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

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

Към профила на Валентин Ейткен

Резултати

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

Код

class Integer
def prime?
self > 1 && 2.upto(Math.sqrt(self)).all? { |n| remainder(n).nonzero? }
end
def prime_factors
prime_factors = 2.upto(abs-1).select { |n| remainder(n).zero? && n.prime? }
prime_factors.map { |prime|
multiple_prime_factors([], self, prime)
}.flatten
end
def harmonic
1.upto(self).map { |i|
1r / i
}.inject :+
end
def digits
self.to_s.split('').map &:to_i
end
private
def multiple_prime_factors multiple_factors, n, prime
if n.remainder(prime).zero?
multiple_prime_factors(multiple_factors << prime, n/prime, prime)
else
multiple_factors
end
end
end
class Array
def frequencies
inject({}) do |frequencies, elem|
frequencies[elem] ||= count(elem)
frequencies
end
end
def average
inject(:+) / count.to_f
end
def drop_every(n)
each_slice(n).map { |slice| slice[0,n-1] }.inject(:+)
end
def combine_with(other)
zip(other).compact.flatten
end
end

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

.FF..F....F.F.

Failures:

  1) Integer#prime_factors constructs an array containing the prime factors in ascending order
     Failure/Error: 61.prime_factors.should  eq [61]
       
       expected: [61]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-1ipybuc/spec.rb:22: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) Integer#prime_factors works with negative numbers
     Failure/Error: (-61).prime_factors.should  eq [61]
       
       expected: [61]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-1ipybuc/spec.rb:28: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)>'

  3) Integer#digits works with negative numbers
     Failure/Error: (-33).digits.should     eq [3, 3]
       
       expected: [3, 3]
            got: [0, 3, 3]
       
       (compared using ==)
     # /tmp/d20131023-4395-1ipybuc/spec.rb:51: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)>'

  4) Array#drop_every drops every n-th element from an array.
     Failure/Error: [].drop_every(2).should eq []
       
       expected: []
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-1ipybuc/spec.rb:88: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)>'

  5) 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-1ipybuc/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.01861 seconds
14 examples, 5 failures

Failed examples:

rspec /tmp/d20131023-4395-1ipybuc/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-1ipybuc/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-1ipybuc/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-1ipybuc/spec.rb:87 # Array#drop_every drops every n-th element from an array.
rspec /tmp/d20131023-4395-1ipybuc/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Валентин обнови решението на 12.10.2013 01:43 (преди около 11 години)

+class Array
+ def frequencies
+ inject({}) do |frequencies, elem|
+ frequencies[elem] ||= count(elem)
+ frequencies
+ end
+ end
+
+ def average
+ inject(:+) / count.to_f
+ end
+
+ def drop_every(n)
+ each_slice(n).map {|slice| slice[0,n-1]}.inject(:+) # flatten
+ end
+
+ def combine_with(other)
+ zip(other).map(&:compact).flatten
+ end
+end

Вальо, много скромно това решение :)

  • На ред 14 трябва да има интервали около пунктуацията, не си спазил конвенцията; + това, коментарът трябва да се махне
  • Ред 18 - map(&:compact) е ненужно; ако искаш да имаш compact, може да го сложиш след flatten

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

+class Integer
+ def prime?
+ self > 1 && 2.upto(Math.sqrt(self)).all? { |n| remainder(n).nonzero? }
+ end
+
+ def prime_factors
+ prime_factors = 2.upto(pred).select { |n| remainder(n).zero? && n.prime? }
+ prime_factors.map { |prime|
+ multiple_prime_factors([], self, prime)
+ }.flatten
+ end
+
+ def harmonic
+
+ end
+
+ def digits
+ self.to_s.split('').map &:to_i
+ end
+
+ private
+ def multiple_prime_factors multiple_factors, n, prime
+ if n.remainder(prime).zero?
+ multiple_prime_factors(multiple_factors << prime, n/prime, prime)
+ else
+ multiple_factors
+ end
+ end
+end
+
class Array
def frequencies
inject({}) do |frequencies, elem|
frequencies[elem] ||= count(elem)
frequencies
end
end
def average
inject(:+) / count.to_f
end
def drop_every(n)
- each_slice(n).map {|slice| slice[0,n-1]}.inject(:+) # flatten
+ each_slice(n).map { |slice| slice[0,n-1] }.inject(:+)
end
def combine_with(other)
- zip(other).map(&:compact).flatten
+ zip(other).compact.flatten
end
end

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

class Integer
def prime?
self > 1 && 2.upto(Math.sqrt(self)).all? { |n| remainder(n).nonzero? }
end
def prime_factors
- prime_factors = 2.upto(pred).select { |n| remainder(n).zero? && n.prime? }
+ prime_factors = 2.upto(abs-1).select { |n| remainder(n).zero? && n.prime? }
prime_factors.map { |prime|
multiple_prime_factors([], self, prime)
}.flatten
end
def harmonic
-
+ 1.upto(self).map { |i|
+ 1r / i
+ }.inject :+
end
def digits
self.to_s.split('').map &:to_i
end
private
def multiple_prime_factors multiple_factors, n, prime
if n.remainder(prime).zero?
multiple_prime_factors(multiple_factors << prime, n/prime, prime)
else
multiple_factors
end
end
end
class Array
def frequencies
inject({}) do |frequencies, elem|
frequencies[elem] ||= count(elem)
frequencies
end
end
def average
inject(:+) / count.to_f
end
def drop_every(n)
each_slice(n).map { |slice| slice[0,n-1] }.inject(:+)
end
def combine_with(other)
zip(other).compact.flatten
end
end