Решение на Първа задача от Моника Илиева

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

Към профила на Моника Илиева

Резултати

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

Код

class Integer
def prime?
(2...self).any? { |number| return false if self % number == 0 }
true
end
end
class Integer
def prime?
(2...self).any? { |number| return false if self % number == 0 }
true
end
def prime_factors
(2..abs).select do |divisor|
divisor.prime? and abs % divisor == 0
end
end
end
class Integer
def harmonic
(1..self).reduce { |sum, number| sum.to_r + 1/number.to_r }
end
end
class Integer
def digits
if self < 0
positive = abs
positive.to_s.split(//).map(&:to_i)
else
to_s.split(//).map(&:to_i)
end
end
end
class Array
def frequencies
counter = Hash.new(0)
each { |element| counter[element] += 1 }
counter.each { |key, value| key and value }
end
end
class Array
def average
reduce(:+).to_f / size
end
end
class Array
def combine_with(other)
if length >= other.length
zip(other).flatten
else
other.zip.flatten
end
end
end

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

FFF.......FFF.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-90seis/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-90seis/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-90seis/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) 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-90seis/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)>'

  5) 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-90seis/spec.rb:98:in `block (3 levels) in <top (required)>'
     # /tmp/d20131023-4395-90seis/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)>'

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

Failed examples:

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

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

Моника обнови решението на 16.10.2013 06:46 (преди над 10 години)

+class Integer
+ def prime?
+ (2...self).any? { |number| return false if self % number == 0 }
+ true
+ end
+end
+
+class Integer
+ def prime?
+ (2...self).any? { |number| return false if self % number == 0 }
+ true
+ end
+
+ def prime_factors
+ (2..abs).select do |divisor|
+ divisor.prime? and abs % divisor == 0
+ end
+ end
+end
+
+class Integer
+ def harmonic
+ (1..self).reduce { |sum, number| sum.to_r + 1/number.to_r }
+ end
+end
+
+class Integer
+ def digits
+ if self < 0
+ positive = abs
+ positive.to_s.split(//).map(&:to_i)
+ else
+ to_s.split(//).map(&:to_i)
+ end
+ end
+end
+
+
+class Array
+ def frequencies
+ counter = Hash.new(0)
+ each { |element| counter[element] += 1 }
+ counter.each { |key, value| key and value }
+ end
+end
+
+class Array
+ def average
+ reduce(:+).to_f / size
+ end
+end
+
+class Array
+ def combine_with(other)
+ if length >= other.length
+ zip(other).flatten
+ else
+ other.zip.flatten
+ end
+ end
+end