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

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

Към профила на Николай Каращранов

Резултати

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

Код

class Integer
def prime?
if self <= 1
return false
end
(2..Math.sqrt(self)).all? { |divisor| self.remainder(divisor) != 0 }
end
def prime_factors
if self == 1
return []
end
factor = (2..self).find { |x| self.remainder(x) == 0 && x.prime?}
[factor] + (self / factor).prime_factors
end
def harmonic
return Rational(1, 1) if self == 1
Rational(1, self) + (self-1).harmonic
end
def digits_reverse
return [] if self == 0
[self % 10] + (self/10).digits_reverse
end
def digits
self.digits_reverse.reverse
end
end
class Array
def frequencies
frequencies_hash = {}
self.each do |x|
if frequencies_hash.has_key?(x)
frequencies_hash[x] += 1
else
frequencies_hash[x] = 1
end
end
frequencies_hash
end
def average
sum = 0
number_count = 0
self.each { |x|
sum += x
number_count += 1}
sum.to_f/number_count
end
def drop_every(n)
i = 1
self.select { |x| (self.index(x) + 1) % n != 0 }
end
end

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

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

Failures:

  1) 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-ntgu8i/solution.rb:13:in `/'
     # /tmp/d20131023-4395-ntgu8i/solution.rb:13:in `prime_factors'
     # /tmp/d20131023-4395-ntgu8i/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)>'

  2) 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-ntgu8i/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)>'

  3) Integer#digits works with negative numbers
     Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
     # /tmp/d20131023-4395-ntgu8i/solution.rb:21

  4) Array#combine_with combines two arrays by alternatingly taking elements
     Failure/Error: [].combine_with([]).should                          eq []
     NoMethodError:
       undefined method `combine_with' for []:Array
     # /tmp/d20131023-4395-ntgu8i/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)>'

  5) Array#combine_with doesn't change the array
     Failure/Error: expect { array.combine_with [1, 2, 3] }.to_not change { array }
     NoMethodError:
       undefined method `combine_with' for [:a, :b, :c]:Array
     # /tmp/d20131023-4395-ntgu8i/spec.rb:115:in `block (3 levels) in <top (required)>'
     # /tmp/d20131023-4395-ntgu8i/spec.rb:115: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.02387 seconds
14 examples, 5 failures

Failed examples:

rspec /tmp/d20131023-4395-ntgu8i/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-ntgu8i/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-ntgu8i/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-ntgu8i/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements
rspec /tmp/d20131023-4395-ntgu8i/spec.rb:113 # Array#combine_with doesn't change the array

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

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

+class Integer
+ def prime?
+ if self <= 1
+ return false
+ end
+ (2..Math.sqrt(self)).all? { |divisor| self.remainder(divisor) != 0 }
+ end
+end

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

class Integer
def prime?
if self <= 1
return false
end
(2..Math.sqrt(self)).all? { |divisor| self.remainder(divisor) != 0 }
end
+ def prime_factors
+ if self == 1
+ return []
+ end
+ factor = (2..self).find { |x| self.remainder(x) == 0 && x.prime?}
+ [factor] + (self / factor).prime_factors
+ end
end

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

class Integer
def prime?
if self <= 1
return false
end
(2..Math.sqrt(self)).all? { |divisor| self.remainder(divisor) != 0 }
end
def prime_factors
if self == 1
return []
end
factor = (2..self).find { |x| self.remainder(x) == 0 && x.prime?}
[factor] + (self / factor).prime_factors
end
+ def harmonic
+ return Rational(1, 1) if self == 1
+ Rational(1, self) + (self-1).harmonic
+ end
end

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

class Integer
def prime?
if self <= 1
return false
end
(2..Math.sqrt(self)).all? { |divisor| self.remainder(divisor) != 0 }
end
def prime_factors
if self == 1
return []
end
factor = (2..self).find { |x| self.remainder(x) == 0 && x.prime?}
[factor] + (self / factor).prime_factors
end
def harmonic
return Rational(1, 1) if self == 1
Rational(1, self) + (self-1).harmonic
end
+ def digits_reverse
+ return [] if self == 0
+ [self % 10] + (self/10).digits_reverse
+ end
+ def digits
+ self.digits_reverse.reverse
+ end
end

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

class Integer
def prime?
if self <= 1
return false
end
(2..Math.sqrt(self)).all? { |divisor| self.remainder(divisor) != 0 }
end
def prime_factors
if self == 1
return []
end
factor = (2..self).find { |x| self.remainder(x) == 0 && x.prime?}
[factor] + (self / factor).prime_factors
end
def harmonic
return Rational(1, 1) if self == 1
Rational(1, self) + (self-1).harmonic
end
def digits_reverse
return [] if self == 0
[self % 10] + (self/10).digits_reverse
end
def digits
self.digits_reverse.reverse
end
+end
+class Array
+ def frequencies
+ frequencies_hash = {}
+ self.each do |x|
+ if frequencies_hash.has_key?(x)
+ frequencies_hash[x] += 1
+ else
+ frequencies_hash[x] = 1
+ end
+ end
+ frequencies_hash
+ end
end

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

class Integer
def prime?
if self <= 1
return false
end
(2..Math.sqrt(self)).all? { |divisor| self.remainder(divisor) != 0 }
end
def prime_factors
if self == 1
return []
end
factor = (2..self).find { |x| self.remainder(x) == 0 && x.prime?}
[factor] + (self / factor).prime_factors
end
def harmonic
return Rational(1, 1) if self == 1
Rational(1, self) + (self-1).harmonic
end
def digits_reverse
return [] if self == 0
[self % 10] + (self/10).digits_reverse
end
def digits
self.digits_reverse.reverse
end
end
class Array
def frequencies
frequencies_hash = {}
self.each do |x|
if frequencies_hash.has_key?(x)
frequencies_hash[x] += 1
else
frequencies_hash[x] = 1
end
end
frequencies_hash
end
+ def average
+ sum = 0
+ number_count = 0
+ self.each { |x|
+ sum += x
+ number_count += 1}
+ sum.to_f/number_count
+ end
+ def drop_every(n)
+ i = 1
+ self.select { |x| (self.index(x) + 1) % n != 0 }
+ end
end