Решение на Първа задача от Моника Ефтимова

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

Към профила на Моника Ефтимова

Резултати

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

Код

class Integer
def digits
n, digits_list = (self < 0) ? -self : self, []
while n > 0
digits_list.push(n % 10)
n /= 10
end
return digits_list.sort
end
end
class Array
def frequencies
return Hash[self.uniq.map { |val| [ val, self.count(val)] }]
end
def average
sum = self.inject(0) { |sum, item| sum + item }
return sum.to_f / self.length
end
def drop_every(n)
list = self - (n - 1).step(self.size - 1, n).map { |i| self[i] }
return list
end
def combine_with(other)
return self.zip(other).flatten.compact
end
end

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

FFFFFF......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-1aca4vn/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]
     NoMethodError:
       undefined method `prime_factors' for 360:Fixnum
     # /tmp/d20131023-4395-1aca4vn/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]
     NoMethodError:
       undefined method `prime_factors' for -4:Fixnum
     # /tmp/d20131023-4395-1aca4vn/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#harmonic returns the n-th harmonic number
     Failure/Error: 1.harmonic.should  eq 1
     NoMethodError:
       undefined method `harmonic' for 1:Fixnum
     # /tmp/d20131023-4395-1aca4vn/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)>'

  5) 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-1aca4vn/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)>'

  6) Integer#digits works with negative numbers
     Failure/Error: (-42).digits.should     eq [4, 2]
       
       expected: [4, 2]
            got: [2, 4]
       
       (compared using ==)
     # /tmp/d20131023-4395-1aca4vn/spec.rb:52: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)>'

  7) Array#combine_with combines two arrays by alternatingly taking elements
     Failure/Error: [].combine_with([1, 2, 3]).should                   eq [1, 2, 3]
       
       expected: [1, 2, 3]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-1aca4vn/spec.rb:105: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.01825 seconds
14 examples, 7 failures

Failed examples:

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

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

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

+class Integer
+ def digits
+ n, digits_list = (self < 0) ? -self : self, []
+ while n > 0
+ digits_list.push(n % 10)
+ n /= 10
+ end
+ return digits_list.sort
+ end
+end
+
+class Array
+ def frequencies
+ return Hash[self.uniq.map { |val| [ val, self.count(val)] }]
+ end
+
+ def average
+ sum = self.inject(0) { |sum, item| sum + item }
+ return sum.to_f / self.length
+ end
+
+ def drop_every(n)
+ list = self - (n - 1).step(self.size - 1, n).map { |i| self[i] }
+ return list
+ end
+
+ def combine_with(other)
+ return self.zip(other).flatten.compact
+ end
+end