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

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

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

Резултати

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

Код

class Integer < Numeric
def prime?
(2...self).map { |divisor| self % divisor != 0 }.all? if self > 0
end
def prime_factors
return [] if self.abs == 1
factor = (2..self.abs).find { |divisor| self % divisor == 0 }
[factor].concat((self.abs / factor).prime_factors)
end
def harmonic
return 1.to_r if self == 1
#sum = 1 / self.to_r + 1 / (self - 1).harmonic
1.upto(self).inject { |sum, addition| sum.to_r + (1/addition.to_r) }
end
def digits
return [self.abs] if self.abs < 10
(self.abs / 10).digits.concat [self.abs % 10]
end
end
class Array
def frequencies
recurrences = Hash.new(0)
each { |key| recurrences[key] += 1 }
return recurrences
end
def average
inject { |sum, addition| sum + addition }.to_f / length
end
def drop_every(n)
result = []
result.replace self
(n - 1).step(result.size - 1, n - 1) { |i| result.delete_at i }
return result
end
def combine_with(other)
result = []
if size > other.size
0.upto(other.size - 1).each { |i| result += [self[i]] + [other[i]] }
result += self.drop(result.size - 1)
else
0.upto(size - 1).each { |i| result += [self[i]] + [other[i]] }
result += other.drop(result.size - 1)
end
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
       
       expected: false
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-s0ity8/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#combine_with combines two arrays by alternatingly taking elements
     Failure/Error: [].combine_with([]).should                          eq []
     ArgumentError:
       attempt to drop negative size
     # /tmp/d20131023-4395-s0ity8/solution.rb:57:in `drop'
     # /tmp/d20131023-4395-s0ity8/solution.rb:57:in `combine_with'
     # /tmp/d20131023-4395-s0ity8/spec.rb:104: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.01852 seconds
14 examples, 2 failures

Failed examples:

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

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

Росен обнови решението на 14.10.2013 08:57 (преди над 10 години)

+class Integer < Numeric
+ def prime?
+
+ (2...self).map { |a| self % a != 0 }.all? if self > 0
+
+ end
+
+ def prime_factors
+
+ return [] if self.abs == 1
+
+ factor = (2..self.abs).find { |a| self % a == 0 }
+ [factor].concat((self.abs / factor).prime_factors)
+
+ end
+
+ def harmonic
+
+ return 1.to_r if self == 1
+ #sum = 1 / self.to_r + 1 / (self - 1).harmonic
+ 1.upto(self).inject { |a, b| a.to_r + (1/b.to_r) }
+
+ end
+
+ def digits
+
+ return [self.abs] if self.abs < 10
+
+ (self.abs / 10).digits.concat [self.abs % 10]
+
+ end
+
+end
+
+class Array
+
+ def frequencies
+ frequencies = Hash.new(0)
+
+ each { |a| frequencies[a] += 1 }
+
+ return frequencies
+ end
+
+ def average
+ inject { |a, b| a + b}.to_f / length
+ end
+
+ def drop_every(n)
+ result = []
+ result.replace self
+
+ (n - 1).step(result.size - 1, n - 1) { |i| result.delete_at i }
+
+ return result
+ end
+
+ def combine_with(other)
+ result = []
+
+ if size > other.size
+ 0.upto(other.size - 1).each { |i| result += [self[i]] + [other[i]] }
+ result += self.drop(result.size - 1)
+ else
+ 0.upto(size - 1).each { |i| result += [self[i]] + [other[i]] }
+ result += other.drop(result.size - 1)
+ end
+ end
+
+end

Росен обнови решението на 15.10.2013 00:14 (преди над 10 години)

class Integer < Numeric
def prime?
-
- (2...self).map { |a| self % a != 0 }.all? if self > 0
-
+ (2...self).map { |divisor| self % divisor != 0 }.all? if self > 0
end
def prime_factors
-
return [] if self.abs == 1
- factor = (2..self.abs).find { |a| self % a == 0 }
+ factor = (2..self.abs).find { |divisor| self % divisor == 0 }
[factor].concat((self.abs / factor).prime_factors)
end
def harmonic
-
return 1.to_r if self == 1
#sum = 1 / self.to_r + 1 / (self - 1).harmonic
- 1.upto(self).inject { |a, b| a.to_r + (1/b.to_r) }
-
+ 1.upto(self).inject { |sum, addition| sum.to_r + (1/addition.to_r) }
end
def digits
-
return [self.abs] if self.abs < 10
(self.abs / 10).digits.concat [self.abs % 10]
-
end
-
end
class Array
-
def frequencies
- frequencies = Hash.new(0)
+ recurrences = Hash.new(0)
- each { |a| frequencies[a] += 1 }
+ each { |key| recurrences[key] += 1 }
- return frequencies
+ return recurrences
end
def average
- inject { |a, b| a + b}.to_f / length
+ inject { |sum, addition| sum + addition }.to_f / length
end
def drop_every(n)
result = []
result.replace self
(n - 1).step(result.size - 1, n - 1) { |i| result.delete_at i }
return result
end
def combine_with(other)
result = []
if size > other.size
0.upto(other.size - 1).each { |i| result += [self[i]] + [other[i]] }
result += self.drop(result.size - 1)
else
0.upto(size - 1).each { |i| result += [self[i]] + [other[i]] }
result += other.drop(result.size - 1)
end
end
-
end