Решение на Първа задача от Лилия Любенова

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

Към профила на Лилия Любенова

Резултати

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

Код

class Integer
def prime?
self < 2 ? is_prime = false : is_prime = true
return false if not is_prime
(2..Math.sqrt(self)).each {|i| is_prime = false if self % i == 0}
return is_prime
end
end
class Integer
def prime_factors
number = self.abs
factors = []
2.upto self.abs do |x| while x.prime? and number % x == 0
factors.push x
number /= x
end
end
return factors
end
end
class Integer
def harmonic
sum = 0
(1..self).each {|x| sum += Rational(1,x)}
return sum
end
end
class Integer
def digits
number = self.abs
digits_array = []
while number > 0
digits_array.push(number % 10)
number /= 10
end
return digits_array.reverse
end
end
class Array
def frequencies
freq = {}
self.each {|i| freq.has_key?(i) ? freq[i] += 1 : freq[i] = 1}
return freq
end
end
class Array
def average
sum = 0.0
self.each {|i| sum += i}
return sum / self.size
end
end
class Array
def drop_every(n)
filtered = []
count = 1
self.each do |x|
filtered.push(x) if count != n
count < n ? count += 1 : count = 1
end
return filtered
end
end
class Array
def combine_with(other)
one = self
combined = []
while one.any? or other.any?
combined.push one.shift unless one.empty?
combined.push other.shift unless other.empty?
end
return combined
end
end

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

....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: []
       
       (compared using ==)
     # /tmp/d20131023-4395-93th3p/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#combine_with doesn't change the array
     Failure/Error: expect { array.combine_with [1, 2, 3] }.to_not change { array }
       result should not have changed, but did change from [:a, :b, :c] to []
     # /tmp/d20131023-4395-93th3p/spec.rb:115: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.04445 seconds
14 examples, 2 failures

Failed examples:

rspec /tmp/d20131023-4395-93th3p/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-93th3p/spec.rb:113 # Array#combine_with doesn't change the array

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

Лилия обнови решението на 16.10.2013 01:17 (преди около 11 години)

+class Integer
+ def prime?
+ self < 2 ? is_prime = false : is_prime = true
+ (2...self).each {|x| is_prime = false if self % x == 0}
+ return is_prime
+ end
+end
+
+class Integer
+ def prime_factors(number = self.abs)
+ factors = []
+ (2..number).each do |x|
+ while x.prime? and number % x == 0
+ factors.push x
+ number /= x
+ end
+ end
+ return factors
+ end
+end
+
+class Integer
+ def harmonic
+ sum = 0
+ (1..self).each {|x| sum += Rational(1,x)}
+ return sum
+ end
+end
+
+class Integer
+ def digits
+ number = self.abs
+ digits_array = []
+ while number > 0
+ digits_array.push(number % 10)
+ number /= 10
+ end
+ return digits_array.reverse
+ end
+end
+
+class Array
+ def frequencies
+ freq = {}
+ self.each {|x| freq.has_key?(x) ? freq[x] += 1 : freq[x] = 1}
+ return freq
+ end
+end
+
+class Array
+ def average
+ sum = 0.0
+ self.each {|x| sum += x}
+ return sum / self.size
+ end
+end
+
+class Array
+ def drop_every(n)
+ filtered = []
+ count = 1
+ self.each do |x|
+ filtered.push(x) if count != n
+ count < n ? count += 1 : count = 1
+ end
+ return filtered
+ end
+end
+
+class Array
+ def combine_with(other)
+ one = self
+ combined = []
+ while one.any? or other.any?
+ combined.push one.shift unless one.empty?
+ combined.push other.shift unless other.empty?
+ end
+ return combined
+ end
+end

Лилия обнови решението на 16.10.2013 15:50 (преди около 11 години)

class Integer
def prime?
self < 2 ? is_prime = false : is_prime = true
- (2...self).each {|x| is_prime = false if self % x == 0}
+ return false if not is_prime
+
+ (2..Math.sqrt(self)).each {|i| is_prime = false if self % i == 0}
return is_prime
end
end
+
class Integer
- def prime_factors(number = self.abs)
+ def prime_factors
+ number = self.abs
factors = []
- (2..number).each do |x|
- while x.prime? and number % x == 0
- factors.push x
- number /= x
+
+ 2.upto self.abs do |x| while x.prime? and number % x == 0
+ factors.push x
+ number /= x
end
end
+
return factors
end
end
+
class Integer
def harmonic
sum = 0
(1..self).each {|x| sum += Rational(1,x)}
return sum
end
end
+
class Integer
def digits
number = self.abs
digits_array = []
+
while number > 0
digits_array.push(number % 10)
number /= 10
end
+
return digits_array.reverse
end
end
+
class Array
def frequencies
freq = {}
- self.each {|x| freq.has_key?(x) ? freq[x] += 1 : freq[x] = 1}
+ self.each {|i| freq.has_key?(i) ? freq[i] += 1 : freq[i] = 1}
return freq
end
end
+
class Array
def average
sum = 0.0
- self.each {|x| sum += x}
+ self.each {|i| sum += i}
return sum / self.size
end
end
+
class Array
def drop_every(n)
filtered = []
count = 1
+
self.each do |x|
filtered.push(x) if count != n
count < n ? count += 1 : count = 1
end
+
return filtered
end
end
+
class Array
def combine_with(other)
one = self
combined = []
+
while one.any? or other.any?
combined.push one.shift unless one.empty?
combined.push other.shift unless other.empty?
end
+
return combined
end
end