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

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

Към профила на Никола Величков

Резултати

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

Код

require 'prime'
class Integer
def prime?
puts Prime.instance.prime?(self.abs)
end
def prime_factors
Prime.prime_division(self.abs).flat_map {|n,i| [n]*i }
end
def harmonic
partialSum = Rational(0)
(1..self).each {|i| partialSum += Rational(1, i)}
puts partialSum
end
def digits
self.abs.to_s.split(//)
end
end
class Array
def frequencies
totalElementsOfKind = Hash.new(0)
self.each { |element| totalElementsOfKind[element] += 1}
totalElementsOfKind
end
def average
self.inject(0.0) {|sum, element| sum += element}/self.size
end
def drop_every(n)
drop = []
(0...self.size).each { |i| drop << self[i] unless (i+1) % n == 0 }
drop
end
def combine_with(other)
result_combination = []
max = self.size > other.size ? self.size : other.size
(0...max).each { |i| result_combination << self[i] << other[i] }
end
end

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

true
F..1/1
FFF......F.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
       
       expected: false
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-qubtgi/spec.rb:3: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#harmonic returns the n-th harmonic number
     Failure/Error: 1.harmonic.should  eq 1
       
       expected: 1
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-qubtgi/spec.rb:35: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 constructs an array containing the digits of a number
     Failure/Error: 0.digits.should      eq [0]
       
       expected: [0]
            got: ["0"]
       
       (compared using ==)
     # /tmp/d20131023-4395-qubtgi/spec.rb:44: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) Integer#digits works with negative numbers
     Failure/Error: (-33).digits.should     eq [3, 3]
       
       expected: [3, 3]
            got: ["3", "3"]
       
       (compared using ==)
     # /tmp/d20131023-4395-qubtgi/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)>'

  5) Array#combine_with combines two arrays by alternatingly taking elements
     Failure/Error: [].combine_with([]).should                          eq []
       
       expected: []
            got: 0...0
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -[]
       +0...0
     # /tmp/d20131023-4395-qubtgi/spec.rb:104: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.01988 seconds
14 examples, 5 failures

Failed examples:

rspec /tmp/d20131023-4395-qubtgi/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-qubtgi/spec.rb:34 # Integer#harmonic returns the n-th harmonic number
rspec /tmp/d20131023-4395-qubtgi/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-qubtgi/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-qubtgi/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

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

+require 'prime'
+
+class Integer
+ def prime?
+ puts Prime.instance.prime?(self.abs)
+ end
+ def prime_factors
+ Prime.prime_division(self.abs).flat_map {|n,i| [n]*i }
+ end
+ def harmonic
+ partialSum = Rational(0)
+ (1..self).each {|i| partialSum += Rational(1, i)}
+ puts partialSum
+ end
+ def digits
+ self.abs.to_s.split(//)
+ end
+end
+class Array
+ def frequencies
+ totalElementsOfKind = Hash.new(0)
+ self.each { |element| totalElementsOfKind[element] += 1}
+ totalElementsOfKind
+ end
+ def average
+ self.inject(0.0) {|sum, element| sum += element}/self.size
+ end
+ def drop_every(n)
+ drop = []
+ (0...self.size).each { |i| drop << self[i] unless (i+1) % n == 0 }
+ drop
+ end
+ def combine_with(other)
+ result_combination = []
+ max = self.size > other.size ? self.size : other.size
+ (0...max).each { |i| result_combination << self[i] << other[i] }
+ end
+end