Николай обнови решението на 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
Към профила на Николай Каращранов
..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