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

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

Към профила на Цветан Коев

Резултати

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

Код

class Integer
def prime?
self > 1 and (2..Math.sqrt(self)).all? { |n| self % n != 0 }
end
def prime_factors
self < 0 ? number = -self : number = self
factors = []
(2..number).each { |divisor| while divisor.prime? and number % divisor == 0
factors.push (divisor)
number = number / divisor
end }
factors
end
def harmonic
sum = 0
(1..self).each { |i| sum = sum + Rational(1,i) }
sum
end
def digits
digits = []
while self / (10**digits.size) > 0
digits.push (self % 10**(digits.size + 1) / 10**digits.size)
end
digits.reverse
end
end
class Array
def frequencies
frequency = {}
each { |n| frequency[n] = self.count(n) }
frequency
end
def average
sum = 0.0
each { |n| sum = sum + n }
sum/self.size
end
def drop_every (n)
dropped_arr = []
each_index { |i| dropped_arr.push(self[i]) if (i + 1) % n != 0 }
dropped_arr
end
def combine_with(other)
combined = []
each_index { |i| combined[2 * i] = self[i] }
other.each_index { |i| combined[2 * i + 1] = other [i] }
combined.compact!
combined
end
end

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

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

Failures:

  1) 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-r9zfou/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) Integer#digits works with negative numbers
     Failure/Error: (-33).digits.should     eq [3, 3]
       
       expected: [3, 3]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-r9zfou/spec.rb:51: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-r9zfou/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.03457 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20131023-4395-r9zfou/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-r9zfou/spec.rb:50 # Integer#digits works with negative numbers
rspec /tmp/d20131023-4395-r9zfou/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Цветан обнови решението на 14.10.2013 23:54 (преди около 11 години)

+class Integer
+ def prime?
+ self > 1 and (2..Math.sqrt(self)).all? { |n| self % n != 0 }
+ end
+
+ def prime_factors
+ self < 0 ? number = -self : number = self
+ factors = []
+ (2..number).each { |divisor| while divisor.prime? and number % divisor == 0
+ factors.push (divisor)
+ number = number / divisor
+ end }
+ factors
+ end
+
+ def harmonic
+ sum = 0
+ (1..self).each { |i| sum = sum + Rational(1,i) }
+ sum
+ end
+
+ def digits
+ digits = []
+ while self / (10**digits.size) > 0
+ digits.push (self % 10**(digits.size+1) / 10**digits.size)
+ end
+ digits.reverse
+ end
+end
+
+class Array
+ def frequencies
+ frequency = {}
+ each { |n| frequency[n] = self.count(n) }
+ frequency
+ end
+
+ def average
+ sum = 0.0
+ each { |n| sum = sum + n }
+ sum/self.size
+ end
+
+ def drop_every (n)
+ dropped_arr = []
+ each_index { |i| dropped_arr.push(self[i]) if (i + 1) % n != 0 }
+ dropped_arr
+ end
+
+ def combine_with(other)
+ combined = []
+ each_index { |i| combined[2 * i] = self[i] }
+ other.each_index { |i| combined[2 * i +1] = other [i] }
+ combined.compact!
+ combined
+ end
+end

Цветан обнови решението на 15.10.2013 00:25 (преди около 11 години)

class Integer
def prime?
self > 1 and (2..Math.sqrt(self)).all? { |n| self % n != 0 }
end
def prime_factors
self < 0 ? number = -self : number = self
factors = []
(2..number).each { |divisor| while divisor.prime? and number % divisor == 0
- factors.push (divisor)
- number = number / divisor
- end }
+ factors.push (divisor)
+ number = number / divisor
+ end }
factors
end
def harmonic
sum = 0
(1..self).each { |i| sum = sum + Rational(1,i) }
sum
end
def digits
digits = []
while self / (10**digits.size) > 0
- digits.push (self % 10**(digits.size+1) / 10**digits.size)
+ digits.push (self % 10**(digits.size + 1) / 10**digits.size)
end
digits.reverse
end
end
class Array
def frequencies
frequency = {}
each { |n| frequency[n] = self.count(n) }
frequency
end
def average
sum = 0.0
each { |n| sum = sum + n }
sum/self.size
end
def drop_every (n)
dropped_arr = []
each_index { |i| dropped_arr.push(self[i]) if (i + 1) % n != 0 }
dropped_arr
end
def combine_with(other)
combined = []
each_index { |i| combined[2 * i] = self[i] }
- other.each_index { |i| combined[2 * i +1] = other [i] }
+ other.each_index { |i| combined[2 * i + 1] = other [i] }
combined.compact!
combined
end
end