Решение на Първа задача от Кристиян Кисимов

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

Към профила на Кристиян Кисимов

Резултати

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

Код

class Integer
def prime?
if self <= 1
return false
end
(2..Math.sqrt(self)).each do |i|
if self % i == 0
return false
end
end
true
end
def prime_factors
array, number = [], (self > 0 ? self : -self)
(2..number).each do |i|
while number % i == 0
array << i
number /= i
end
end
array
end
def harmonic
rational = Rational(0, 1)
(1..self).each do |i|
rational += Rational(1, i)
end
rational
end
def digits
array, number = [], (self > 0 ? self : -self)
while number != 0
array << number % 10
number /=10
end
self == 0 ? nil : array.reverse
end
end
class Array
def frequencies
hash = {}
each do |i|
if hash.has_key?(i)
hash[i] += 1
else
hash[i] = 1
end
end
hash
end
def average
sum, count = 0.0, 0
each do |i|
sum += i
count += 1
end
sum / count
end
def drop_every(n)
array = n != 0 ? [] : self
each do |i|
if n != 0 and (index(i) + 1) % n != 0
array << i
end
end
self.empty? ? nil : array
end
def combine_with(other)
array = []
len = self.length < other.length ? other.length : self.length
(0..len-1).each do |i|
array << self[i]
array << other[i]
end
array.compact
end
end

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

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

Failures:

  1) Integer#digits constructs an array containing the digits of a number
     Failure/Error: 0.digits.should      eq [0]
       
       expected: [0]
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-15zu3j/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)>'

  2) Array#drop_every drops every n-th element from an array.
     Failure/Error: [].drop_every(2).should eq []
       
       expected: []
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-15zu3j/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)>'

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

Failed examples:

rspec /tmp/d20131023-4395-15zu3j/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-15zu3j/spec.rb:87 # Array#drop_every drops every n-th element from an array.
rspec /tmp/d20131023-4395-15zu3j/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Кристиян обнови решението на 14.10.2013 16:08 (преди около 11 години)

+class Integer
+ def prime?
+ if self <= 1
+ return false
+ end
+ (2..Math.sqrt(self)).each do |i|
+ if self % i == 0
+ return false
+ end
+ end
+ true
+ end
+ def prime_factors
+ array, number = [], (self > 0 ? self : -self)
+ (2..number).each do |i|
+ while number % i == 0
+ array << i
+ number /= i
+ end
+ end
+ array
+ end
+ def harmonic
+ rational = Rational(0, 1)
+ (1..self).each do |i|
+ rational += Rational(1, i)
+ end
+ rational
+ end
+ def digits
+ array, number = [], (self > 0 ? self : -self)
+ while number != 0
+ array << number % 10
+ number /=10
+ end
+ self == 0 ? nil : array.reverse
+ end
+end
+
+class Array
+ def frequencies
+ hash = {}
+ each do |i|
+ if hash.has_key?(i)
+ hash[i] += 1
+ else
+ hash[i] = 1
+ end
+ end
+ hash
+ end
+ def average
+ sum, count = 0.0, 0
+ each do |i|
+ sum += i
+ count += 1
+ end
+ sum / count
+ end
+ def drop_every(n)
+ array = n != 0 ? [] : self
+ each do |i|
+ if n != 0 and (index(i) + 1) % n != 0
+ array << i
+ end
+ end
+ self.empty? ? nil : array
+ end
+ def combine_with(other)
+ array = []
+ len = self.length < other.length ? other.length : self.length
+ (0..len-1).each do |i|
+ array << self[i]
+ array << other[i]
+ end
+ array.compact
+ end
+end