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

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

Към профила на Борислава Йорданова

Резултати

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

Код

class Integer
def prime?
if self < 2
return false
end
(2..self-1).all?{ |number| self % number != 0 }
end
def prime_factors
2.upto(self).select{ |number| number.prime? and self % number == 0 }
end
def find_harmonic_number
1 / self.to_r
end
def harmonic
sum = 0.to_r
(1..self).each { |i| sum = sum + i.find_harmonic_number }
sum
end
def digits
to_s.scan(/./).map { |number| number.to_i }
end
end
class Array
def frequencies
hash = {}
self.each do |number|
if hash.has_key?(number)
hash[ number ] += 1
else
hash[ number ] = 1
end
end
hash
end
def average
reduce(:+).to_f / length
end
def drop_every(n)
find_all{ |i| i % n != 0 }
end
def combine_with(other)
if length > other.length
zip(other).flatten.compact
else
other.zip(self).flatten.compact
end
end
end

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

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

Failures:

  1) Integer#prime_factors constructs an array containing the prime factors in ascending order
     Failure/Error: 360.prime_factors.should eq [2, 2, 2, 3, 3, 5]
       
       expected: [2, 2, 2, 3, 3, 5]
            got: [2, 3, 5]
       
       (compared using ==)
     # /tmp/d20131023-4395-dqno06/spec.rb:19: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: (-4).prime_factors.should   eq [2, 2]
       
       expected: [2, 2]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-dqno06/spec.rb:27: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-dqno06/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: [42].drop_every(2).should eq [42]
       
       expected: [42]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-dqno06/spec.rb:89: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: [:a, :b, :c].combine_with([1, 2, 3]).should         eq [:a, 1, :b, 2, :c, 3]
       
       expected: [:a, 1, :b, 2, :c, 3]
            got: [1, :a, 2, :b, 3, :c]
       
       (compared using ==)
     # /tmp/d20131023-4395-dqno06/spec.rb:107: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.02145 seconds
14 examples, 5 failures

Failed examples:

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

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

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

+class Integer
+ def prime?
+ if self < 2
+ return false
+ end
+ (2..self-1).all?{ |number| self % number != 0 }
+ end
+
+ def prime_factors
+ 2.upto(self).select{ |number| number.prime? and self % number == 0 }
+ end
+
+ def find_harmonic_number
+ 1 / self.to_r
+ end
+
+ def harmonic
+ sum = 0.to_r
+ (1..self).each { |i| sum = sum + i.find_harmonic_number }
+ sum
+ end
+
+ def digits
+ to_s.scan(/./).map { |number| number.to_i }
+ end
+end
+
+class Array
+ def frequencies
+ hash = {}
+ self.each do |number|
+ if hash.has_key?(number)
+ hash[ number ] += 1
+ else
+ hash[ number ] = 1
+ end
+ end
+ hash
+ end
+
+ def average
+ reduce(:+).to_f / length
+ end
+
+ def drop_every(n)
+ find_all{ |i| i % n != 0 }
+ end
+
+ def combine_with(other)
+ if length > other.length
+ zip(other).flatten.compact
+ else
+ other.zip(self).flatten.compact
+ end
+ end
+end