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

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

Към профила на Станислав Станчев

Резултати

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

Код

class Integer
def prime?
return false if abs < 2
(2..abs/2).each { |n| return false if abs % n == 0 }
true
end
def prime_factors
factors = []
(2..abs).each { |n| factors.concat(single_prime_divisors(abs, n)) }
factors
end
def single_prime_divisors(number, divisor)
divisors = []
while number % divisor == 0 and divisor.prime?
divisors << divisor
number /= divisor
end
divisors
end
def harmonic
harmonic_number, number = Rational(1), abs
while number > 1
harmonic_number += Rational(1, number)
number -= 1
end
harmonic_number
end
def digits
abs.to_s.split('').map { |digit| digit.to_i}
end
end
class Array
def average
sum = 0
each { |n| sum = sum + n }
sum / length.to_f
end
def frequencies
hash = Hash.new(0) # NoMethodError for + if hash = {}
each { |item| hash[item] += 1 }
hash
end
def drop_every(n)
reverse_each do |item|
delete_at(index(item)) if (index(item) + 1) % n == 0
end
end
def combine_with(other)
return other if empty?
zip(other).flatten.delete_if { |item| item.nil? }
end
end

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

F..........FF.

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-kv611o/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) Array#drop_every doesn't change the array
     Failure/Error: expect { array.drop_every(3) }.to_not change { array }
       result should not have changed, but did change from [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] to [1, 2, 4, 5, 7, 8, 10]
     # /tmp/d20131023-4395-kv611o/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)>'

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

Failed examples:

rspec /tmp/d20131023-4395-kv611o/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-kv611o/spec.rb:96 # Array#drop_every doesn't change the array
rspec /tmp/d20131023-4395-kv611o/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Станислав обнови решението на 16.10.2013 10:36 (преди над 10 години)

+class Integer
+ def prime?
+ return false if abs < 2
+ (2..abs/2).each { |n| return false if abs % n == 0 }
+ true
+ end
+
+ def prime_factors
+ factors = []
+ (2..abs).each { |n| factors.concat(single_prime_divisors(abs, n)) }
+ factors
+ end
+
+ def single_prime_divisors(number, divisor)
+ divisors = []
+ while number % divisor == 0 and divisor.prime?
+ divisors << divisor
+ number /= divisor
+ end
+ divisors
+ end
+
+ def harmonic
+ harmonic_number, number = Rational(1), abs
+ while number > 1
+ harmonic_number += Rational(1, number)
+ number -= 1
+ end
+ harmonic_number
+ end
+
+ def digits
+ abs.to_s.split('').map { |digit| digit.to_i}
+ end
+end
+
+class Array
+ def average
+ sum = 0
+ each { |n| sum = sum + n }
+ sum / length.to_f
+ end
+
+ def frequencies
+ hash = Hash.new(0) # NoMethodError for + if hash = {}
+ each { |item| hash[item] += 1 }
+ hash
+ end
+
+ def drop_every(n)
+ reverse_each do |item|
+ delete_at(index(item)) if (index(item) + 1) % n == 0
+ end
+ end
+
+ def combine_with(other)
+ return other if empty?
+ zip(other).flatten.delete_if { |item| item.nil? }
+ end
+end