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

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

Към профила на Веселин Генадиев

Резултати

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

Код

class Integer
def prime?
true if self == 2
counter = 2
while counter <= Math.sqrt(self)
false if self % counter == 0
counter += 1
end
true
end
def prime_factors
list = []
argument = self
while argument.abs > 1
list.push((2..argument).select{|x| (x.prime? && argument % x == 0)}.first)
argument /= list.last
end
list
end
def harmonic
harmonic_number = Rational(0, 1)
counter = 1
while counter <= self
harmonic_number += Rational(1, counter)
counter += 1
end
harmonic_number
end
def digits
digits_list = []
number_positive = self.abs
while number_positive > 0
digits_list.push(number_positive % 10)
number_positive /= 10
end
digits_list.reverse
end
end
class Array
def frequencies
frequencies_hash = {}
self.each{|x| frequencies_hash[x] ?
frequencies_hash[x] += 1 : frequencies_hash[x] = 1}
frequencies_hash
end
def average
sum = 0.0
self.each{|x| sum += x}
avg = sum / self.length
end
def drop_every(n)
nth_dropped_list = []
counter = 0
while counter < self.length
nth_dropped_list.push(self[counter]) unless (counter + 1) % n == 0
counter += 1
end
nth_dropped_list
end
def combine_with(other)
list = []
bigger = [self, other].max{|x, y| x.length <=> y.length}
(0..[self.length, other.length].min - 1).
each{|i| list.push(self[i], other[i])}
(list.length / 2..bigger.length - 1).each{|i| list.push(bigger[i])}
list
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
     Math::DomainError:
       Numerical argument is out of domain - "sqrt"
     # /tmp/d20131023-4395-gn8r0r/solution.rb:5:in `sqrt'
     # /tmp/d20131023-4395-gn8r0r/solution.rb:5:in `prime?'
     # /tmp/d20131023-4395-gn8r0r/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#prime_factors works with negative numbers
     Failure/Error: (-4).prime_factors.should   eq [2, 2]
     TypeError:
       nil can't be coerced into Fixnum
     # /tmp/d20131023-4395-gn8r0r/solution.rb:17:in `/'
     # /tmp/d20131023-4395-gn8r0r/solution.rb:17:in `prime_factors'
     # /tmp/d20131023-4395-gn8r0r/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 constructs an array containing the digits of a number
     Failure/Error: 0.digits.should      eq [0]
       
       expected: [0]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-gn8r0r/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)>'

Finished in 0.07115 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20131023-4395-gn8r0r/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-gn8r0r/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-gn8r0r/spec.rb:43 # Integer#digits constructs an array containing the digits of a number

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

Веселин обнови решението на 16.10.2013 03:21 (преди над 10 години)

+class Integer
+ def prime?
+ true if self == 2
+ counter = 2
+ while counter <= Math.sqrt(self)
+ false if self % counter == 0
+ counter += 1
+ end
+ true
+ end
+
+ def prime_factors
+ list = []
+ argument = self
+ while argument.abs > 1
+ list.push((2..argument).select{|x| (x.prime? && argument % x == 0)}.first)
+ argument /= list.last
+ end
+ list
+ end
+
+ def harmonic
+ harmonic_number = Rational(0, 1)
+ counter = 1
+ while counter <= self
+ harmonic_number += Rational(1, counter)
+ counter += 1
+ end
+ harmonic_number
+ end
+
+ def digits
+ digits_list = []
+ number_positive = self.abs
+ while number_positive > 0
+ digits_list.push(number_positive % 10)
+ number_positive /= 10
+ end
+ digits_list.reverse
+ end
+end
+
+class Array
+ def frequencies
+ frequencies_hash = {}
+ self.each{|x| frequencies_hash[x] ?
+ frequencies_hash[x] += 1 : frequencies_hash[x] = 1}
+ frequencies_hash
+ end
+
+ def average
+ sum = 0.0
+ self.each{|x| sum += x}
+ avg = sum / self.length
+ end
+
+ def drop_every(n)
+ nth_dropped_list = []
+ counter = 0
+ while counter < self.length
+ nth_dropped_list.push(self[counter]) unless (counter + 1) % n == 0
+ counter += 1
+ end
+ nth_dropped_list
+ end
+
+ def combine_with(other)
+ list = []
+ bigger = [self, other].max{|x, y| x.length <=> y.length}
+ (0..[self.length, other.length].min - 1).
+ each{|i| list.push(self[i], other[i])}
+ (list.length / 2..bigger.length - 1).each{|i| list.push(bigger[i])}
+ list
+ end
+end