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

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

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

Резултати

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

Код

class Integer
def prime()
return false if self < 0
return true if self < 3
(2.. Math.sqrt(self.abs)).each do |var|
if self%var == 0
return false
end
end
return true
end
def prime_factors()
primes = []
return self if self.prime
(2 .. self).each do |d|
if self % d == 0 and d.prime
primes.push(d)
return primes.push((self/d ) . prime_factors)
end
end
end
def harmonic()
if self < 0
return false
end
harmNum = Rational(1,1)
(2.. self).each do |var|
harmNum += Rational(1,var)
end
return harmNum
end
def digits()
currNum = self
digitsArray = []
while currNum != 0
digitsArray.push(currNum % 10)
currNum /= 10
end
return digitsArray.reverse
end
end
class Array
def average()
totalValue = 0.0
(0... self.size).each do |var|
totalValue+=self[var]
end
return totalValue/self.size
end
def frequencies()
frequenciesArray = Hash.new
(0... self.size).each do |var|
if frequenciesArray[self[var]] == nil
frequenciesArray[self[var]] = 0
end
frequenciesArray[self[var]] += 1
end
return frequenciesArray
end
def drop_every(n)
dropped = []
(0... self.size).each do |var|
if (var+1)%n != 0
dropped.push(self[var])
end
end
return dropped
end
def combine_with(other)
max = self.size > other.size ? self.size : other.size
combination = []
(0...max).each do |i|
combination.push(self[i]) if self[i]
combination.push(other[i]) if other[i]
end
return combination
end
end

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

FFF.FF......F.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
     NoMethodError:
       undefined method `prime?' for -13:Fixnum
     # /tmp/d20131023-4395-w0jrsn/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 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, [2, [2, [3, [3, 5]]]]]
       
       (compared using ==)
     # /tmp/d20131023-4395-w0jrsn/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)>'

  3) Integer#prime_factors works with negative numbers
     Failure/Error: (-4).prime_factors.should   eq [2, 2]
       
       expected: [2, 2]
            got: 2..-4
       
       (compared using ==)
     # /tmp/d20131023-4395-w0jrsn/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)>'

  4) 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-w0jrsn/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)>'

  5) Integer#digits works with negative numbers
     Failure/Error: (-33).digits.should     eq [3, 3]
     Timeout::Error:
       execution expired
     # /tmp/d20131023-4395-w0jrsn/solution.rb:35:in `digits'
     # /tmp/d20131023-4395-w0jrsn/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)>'

  6) Array#combine_with combines two arrays by alternatingly taking elements
     Failure/Error: [:a, :b, :c].combine_with([1, nil, 3]).should       eq [:a, 1, :b, nil, :c, 3]
       
       expected: [:a, 1, :b, nil, :c, 3]
            got: [:a, 1, :b, :c, 3]
       
       (compared using ==)
     # /tmp/d20131023-4395-w0jrsn/spec.rb:110: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 7.32 seconds
14 examples, 6 failures

Failed examples:

rspec /tmp/d20131023-4395-w0jrsn/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-w0jrsn/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-w0jrsn/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-w0jrsn/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-w0jrsn/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-w0jrsn/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Йордан обнови решението на 16.10.2013 15:04 (преди около 11 години)

+class Integer
+ def prime()
+ return false if self < 0
+ return true if self < 3
+ (2.. Math.sqrt(self.abs)).each do |var|
+ if self%var == 0
+ return false
+ end
+ end
+ return true
+ end
+ def prime_factors()
+ primes = []
+ return self if self.prime
+ (2 .. self).each do |d|
+ if self % d == 0 and d.prime
+ primes.push(d)
+ return primes.push((self/d ) . prime_factors)
+ end
+ end
+ end
+ def harmonic()
+ if self < 0
+ return false
+ end
+ harmNum = Rational(1,1)
+ (2.. self).each do |var|
+ harmNum += Rational(1,var)
+ end
+ return harmNum
+ end
+ def digits()
+ currNum = self
+ digitsArray = []
+ while currNum != 0
+ digitsArray.push(currNum % 10)
+ currNum /= 10
+ end
+ return digitsArray.reverse
+ end
+end
+class Array
+ def average()
+ totalValue = 0.0
+ (0... self.size).each do |var|
+ totalValue+=self[var]
+ end
+ return totalValue/self.size
+ end
+ def frequencies()
+ frequenciesArray = Hash.new
+ (0... self.size).each do |var|
+ if frequenciesArray[self[var]] == nil
+ frequenciesArray[self[var]] = 0
+ end
+ frequenciesArray[self[var]] += 1
+ end
+ return frequenciesArray
+ end
+ def drop_every(n)
+ dropped = []
+ (0... self.size).each do |var|
+ if (var+1)%n != 0
+ dropped.push(self[var])
+ end
+ end
+ return dropped
+ end
+end

Йордан обнови решението на 16.10.2013 15:21 (преди около 11 години)

class Integer
def prime()
return false if self < 0
return true if self < 3
(2.. Math.sqrt(self.abs)).each do |var|
if self%var == 0
return false
end
end
return true
end
def prime_factors()
primes = []
return self if self.prime
(2 .. self).each do |d|
if self % d == 0 and d.prime
primes.push(d)
return primes.push((self/d ) . prime_factors)
end
end
end
def harmonic()
if self < 0
return false
end
harmNum = Rational(1,1)
(2.. self).each do |var|
harmNum += Rational(1,var)
end
return harmNum
end
def digits()
currNum = self
digitsArray = []
while currNum != 0
digitsArray.push(currNum % 10)
currNum /= 10
end
return digitsArray.reverse
end
end
class Array
def average()
totalValue = 0.0
(0... self.size).each do |var|
totalValue+=self[var]
end
return totalValue/self.size
end
def frequencies()
frequenciesArray = Hash.new
(0... self.size).each do |var|
if frequenciesArray[self[var]] == nil
frequenciesArray[self[var]] = 0
end
frequenciesArray[self[var]] += 1
end
return frequenciesArray
end
def drop_every(n)
dropped = []
(0... self.size).each do |var|
if (var+1)%n != 0
dropped.push(self[var])
end
end
return dropped
end
+ def combine_with(other)
+ max = self.size > other.size ? self.size : other.size
+ combination = []
+ (0...max).each do |i|
+ combination.push(self[i]) if self[i]
+ combination.push(other[i]) if other[i]
+ end
+ return combination
+ end
end