Решение на Първа задача от Августина Тенчева

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

Към профила на Августина Тенчева

Резултати

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

Код

class Integer
def prime?
return nil if self <= 0 or self == []
return false if self == 1
(2..self / 2).each do |i|
return false if self % i == 0
end
return true
end
def prime_factors
(2..abs).select{ |i| i.prime? and self % i == 0 }
end
end
class Integer
def harmonic
if self == 1 then return 1
return Rational(1, self) + harmonic(self-1)
end
end
end
class Integer
def digits
number = abs
number.to_s.split(//).map{ |char| char.to_i}
end
end
class Array
def frequencies
frequency = Hash.new(0)
self.each { |item| frequency[item] += 1 }
return frequency
end
end
class Array
def average
inject {|sum, element| sum + element}.to_f / size
end
end
class Array
def combine_with(other)
return self.zip(other).flatten.compact
end
end

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

FFFF......FFF.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
       
       expected: false
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-1mdyenc/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, 3, 5]
       
       (compared using ==)
     # /tmp/d20131023-4395-1mdyenc/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]
       
       (compared using ==)
     # /tmp/d20131023-4395-1mdyenc/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: 2.harmonic.should  eq 3/2r
       
       expected: (3/2)
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-1mdyenc/spec.rb:36: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#drop_every drops every n-th element from an array.
     Failure/Error: [].drop_every(2).should eq []
     NoMethodError:
       undefined method `drop_every' for []:Array
     # /tmp/d20131023-4395-1mdyenc/spec.rb:88: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#drop_every doesn't change the array
     Failure/Error: expect { array.drop_every(3) }.to_not change { array }
     NoMethodError:
       undefined method `drop_every' for [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:Array
     # /tmp/d20131023-4395-1mdyenc/spec.rb:98:in `block (3 levels) in <top (required)>'
     # /tmp/d20131023-4395-1mdyenc/spec.rb:98: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-1mdyenc/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.02102 seconds
14 examples, 7 failures

Failed examples:

rspec /tmp/d20131023-4395-1mdyenc/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-1mdyenc/spec.rb:18 # Integer#prime_factors constructs an array containing the prime factors in ascending order
rspec /tmp/d20131023-4395-1mdyenc/spec.rb:26 # Integer#prime_factors works with negative numbers
rspec /tmp/d20131023-4395-1mdyenc/spec.rb:34 # Integer#harmonic returns the n-th harmonic number
rspec /tmp/d20131023-4395-1mdyenc/spec.rb:87 # Array#drop_every drops every n-th element from an array.
rspec /tmp/d20131023-4395-1mdyenc/spec.rb:96 # Array#drop_every doesn't change the array
rspec /tmp/d20131023-4395-1mdyenc/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Августина обнови решението на 16.10.2013 16:58 (преди около 11 години)

+class Integer
+ def prime?
+ return nil if self <= 0 or self == []
+ return false if self == 1
+ (2..self / 2).each do |i|
+ return false if self % i == 0
+ end
+ return true
+ end
+
+ def prime_factors
+ (2..abs).select{ |i| i.prime? and self % i == 0 }
+ end
+end
+
+
+class Integer
+ def harmonic
+ if self == 1 then return 1
+ return Rational(1, self) + harmonic(self-1)
+ end
+ end
+end
+
+
+class Integer
+ def digits
+ number = abs
+ number.to_s.split(//).map{ |char| char.to_i}
+ end
+end
+
+
+class Array
+ def frequencies
+ frequency = Hash.new(0)
+ self.each { |item| frequency[item] += 1 }
+ return frequency
+ end
+end
+
+
+class Array
+ def average
+ inject {|sum, element| sum + element}.to_f / size
+ end
+end
+
+
+class Array
+ def combine_with(other)
+ return self.zip(other).flatten.compact
+ end
+end
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+