Решение на Първа задача от Мария Терзиева

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

Към профила на Мария Терзиева

Резултати

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

Код

class Integer
def prime?
return false if self < 0
2.upto(self - 1).all? { |i| remainder(i).nonzero? }
end
def prime_factors
prime_factors_array, upper_limit = [], abs
2.upto(upper_limit).each do |i|
while upper_limit.remainder(i).zero?
upper_limit /= i
prime_factors_array << i
end
end
prime_factors_array
end
def harmonic
0.upto(self).inject { |sum, i| sum + 1 / Rational(i) }
end
def digits
abs.to_s.split('').map { |string_digit| string_digit.to_i }
end
end
class Array
def frequencies
frequencies_hash = Hash.new(0)
each { |value| frequencies_hash[value] += 1 }
frequencies_hash
end
def average
reduce(:+).to_f / size
end
def drop_every(n)
select.with_index { |_, index| (index + 1).remainder(n).nonzero? }
end
def combine_with(other)
zip(other).flatten.compact
end
end

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

F...........F.

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: 0.prime?.should   eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-xlg1hx/spec.rb:4: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#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-xlg1hx/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.02225 seconds
14 examples, 2 failures

Failed examples:

rspec /tmp/d20131023-4395-xlg1hx/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-xlg1hx/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Мария обнови решението на 14.10.2013 15:51 (преди над 10 години)

+class Integer
+ def prime?
+ 2.upto(self - 1).all? { |n| self.remainder(n).nonzero? }
+ end
+
+ def prime_factors
+ array, number = [], self.abs
+ 2.upto(number).each do |n|
+ number /= n and array << n while number.remainder(n).zero?
+ end
+ return array
+ end
+
+ def harmonic
+ 0.upto(self).inject { |sum, value| sum + 1 / Rational(value) }
+ end
+
+ def digits
+ self.abs.to_s.split('').map { |n| n.to_i }
+ end
+end
+
+class Array
+ def frequencies
+ hash = Hash.new(0)
+ self.each { |value| hash[value] += 1 }
+ return hash
+ end
+
+ def average
+ self.reduce(:+).to_f / self.size
+ end
+
+ def drop_every(n)
+ self.select.with_index { |_, index| (index + 1).remainder(n).nonzero? }
+ end
+
+ def combine_with(other)
+ self.zip(other).flatten.compact
+ end
+end

Мария обнови решението на 14.10.2013 23:28 (преди над 10 години)

class Integer
def prime?
- 2.upto(self - 1).all? { |n| self.remainder(n).nonzero? }
+ 2.upto(self - 1).all? { |i| remainder(i).nonzero? }
end
def prime_factors
- array, number = [], self.abs
- 2.upto(number).each do |n|
- number /= n and array << n while number.remainder(n).zero?
+ prime_factors_array, upper_limit = [], abs
+ 2.upto(upper_limit).each do |i|
+ while upper_limit.remainder(i).zero?
+ upper_limit /= i
+ prime_factors_array << i
+ end
end
- return array
+ prime_factors_array
end
def harmonic
- 0.upto(self).inject { |sum, value| sum + 1 / Rational(value) }
+ 0.upto(self).inject { |sum, i| sum + 1 / Rational(i) }
end
def digits
- self.abs.to_s.split('').map { |n| n.to_i }
+ abs.to_s.split('').map { |string_digit| string_digit.to_i }
end
end
class Array
def frequencies
- hash = Hash.new(0)
- self.each { |value| hash[value] += 1 }
- return hash
+ frequencies_hash = Hash.new(0)
+ each { |value| frequencies_hash[value] += 1 }
+ frequencies_hash
end
def average
- self.reduce(:+).to_f / self.size
+ reduce(:+).to_f / size
end
def drop_every(n)
- self.select.with_index { |_, index| (index + 1).remainder(n).nonzero? }
+ select.with_index { |_, index| (index + 1).remainder(n).nonzero? }
end
def combine_with(other)
- self.zip(other).flatten.compact
+ zip(other).flatten.compact
end
end

Мария обнови решението на 14.10.2013 23:53 (преди над 10 години)

class Integer
def prime?
+ return false if self < 0
2.upto(self - 1).all? { |i| remainder(i).nonzero? }
end
def prime_factors
prime_factors_array, upper_limit = [], abs
2.upto(upper_limit).each do |i|
while upper_limit.remainder(i).zero?
upper_limit /= i
prime_factors_array << i
end
end
prime_factors_array
end
def harmonic
0.upto(self).inject { |sum, i| sum + 1 / Rational(i) }
end
def digits
abs.to_s.split('').map { |string_digit| string_digit.to_i }
end
end
class Array
def frequencies
frequencies_hash = Hash.new(0)
each { |value| frequencies_hash[value] += 1 }
frequencies_hash
end
def average
reduce(:+).to_f / size
end
def drop_every(n)
select.with_index { |_, index| (index + 1).remainder(n).nonzero? }
end
def combine_with(other)
zip(other).flatten.compact
end
end