Решение на Първа задача от Иван Проданов

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

Към профила на Иван Проданов

Резултати

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

Код

class Integer
def prime?
return true if self == 2
return false if self.even? or self <= 1
sqrt = Math.sqrt(self).to_i
(2..sqrt).step(2).none?{|divisor| self.modulo(divisor).zero?}
end
def prime_factors (previous_divisor=2)
return nil if self.abs <= 1
sqrt = Math.sqrt(self.abs)
divisor = (previous_divisor..sqrt).find{|x| self.abs.modulo(x).zero?}
divisor ? [divisor] + (self.abs/divisor).prime_factors(divisor) : [self.abs]
end
def digits
self.to_s.split('').map{|digit| digit.to_i }
end
def harmonic
(2..self).inject(1){|sum, x| sum + 1/x.to_r}
end
end
class Array
def frequencies
freq = Hash.new(0)
self.each{|item| freq[item] += 1}
freq
end
def average
self.inject(:+)/self.length.to_f
end
def drop_every (n)
(0...self.length).step(n).inject([]) {|result, i| result + self[i...i+n-1]}
end
def combine_with (other)
len = [self.length, other.length]
(0...len.min).inject([]){|sh, index| sh << self[index] << other[index]} +
(len.min...len.max).map{|index| self[index] or other[index]}
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: 27.prime?.should  eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-20engd/spec.rb:10: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#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-20engd/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)>'

Finished in 0.01765 seconds
14 examples, 2 failures

Failed examples:

rspec /tmp/d20131023-4395-20engd/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-20engd/spec.rb:50 # Integer#digits works with negative numbers

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

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

+class Integer
+ def prime?
+ return true if self == 2
+ return false if self.even? or self <= 1
+ sqrt = Math.sqrt(self).to_i
+ (2..sqrt).step(2).none?{|divisor| self.modulo(divisor).zero?}
+ end
+
+ def prime_factors (previous_divisor=2)
+ return nil if self.abs <= 1
+ sqrt = Math.sqrt(self.abs)
+ divisor = (previous_divisor..sqrt).find{|x| self.abs.modulo(x).zero?}
+ divisor ? [divisor] + (self.abs/divisor).prime_factors(divisor) : [self.abs]
+ end
+
+ def digits
+ self.to_s.split('').map{|digit| digit.to_i }
+ end
+
+ def harmonic
+ (2..self).inject(1){|sum, x| sum + 1/x.to_r}
+ end
+end
+
+class Array
+ def frequencies
+ freq = Hash.new(0)
+ self.each{|item| freq[item] += 1}
+ freq
+ end
+
+ def average
+ self.inject(:+)/self.length.to_f
+ end
+
+ def drop_every (n)
+ (0...self.length).step(n).inject([]) {|result, i| result + self[i...i+n-1]}
+ end
+
+ def combine_with (other)
+ len = [self.length, other.length]
+ (0...len.min).inject([]){|sh, index| sh << self[index] << other[index]} +
+ (len.min...len.max).map{|index| self[index] or other[index]}
+ end
+end