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

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

Към профила на Венцислав Велков

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 10 успешни тест(а)
  • 4 неуспешни тест(а)

Код

class Integer
def prime?
2.upto(pred).all? { |i| remainder(i).nonzero? } and self > 0
end
end
class Integer
def prime_factors
self2 = self.abs
factors = []
while self2 != 1
factors << (2..self2).detect { |i| i.prime? and self2 % i == 0 }
self2 = self2 / factors.last
end
factors
end
end
class Integer
def harmonic
sum = Rational(0)
(1..self).each { |i| sum = sum + Rational(1, i) }
sum
end
end
class Integer
def digits
array_for_digits = []
self2 = self.abs
while self2 != 0
array_for_digits << self2 % 10
self2 = self2 / 10
end
array_for_digits.reverse
end
end
class Array
def average
sum = Float(0)
self.each { |i| sum = sum + i }
return sum / self.length
end
end
class Array
def drop_every(n)
arraycopy = self
k = n
while n <= arraycopy.length
arraycopy.delete_at(n-1)
n = n + k - 1
end
arraycopy
end
end
class Array
def frequencies
ar = self.uniq
hash = {}
(0..(ar.length - 1)).each do |i|
hash = hash.merge({ar[i] => self.count(ar[i])})
end
hash
end
end
class Array
def combine_with(arr)
new_array = []
i = 0
while ( self[i] or arr[i] )
new_array << self[i] << arr[i]
i = i + 1
end
new_array.compact
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: 1.prime?.should   eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-oiu9rb/spec.rb:5: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 constructs an array containing the digits of a number
     Failure/Error: 0.digits.should      eq [0]
       
       expected: [0]
            got: []
       
       (compared using ==)
     # /tmp/d20131023-4395-oiu9rb/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)>'

  3) Array#drop_every doesn't change the array
     Failure/Error: expect { array.drop_every(3) }.to_not change { array }
       result should not have changed, but did change from [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] to [1, 2, 4, 5, 7, 8, 10]
     # /tmp/d20131023-4395-oiu9rb/spec.rb:98: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)>'

  4) 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-oiu9rb/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.02395 seconds
14 examples, 4 failures

Failed examples:

rspec /tmp/d20131023-4395-oiu9rb/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-oiu9rb/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-oiu9rb/spec.rb:96 # Array#drop_every doesn't change the array
rspec /tmp/d20131023-4395-oiu9rb/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Венцислав обнови решението на 15.10.2013 15:38 (преди над 10 години)

+class Integer
+ def prime?
+ 2.upto(pred).all? { |i| remainder(i).nonzero? } and self > 0
+ end
+end
+
+class Integer
+ def prime_factors
+ self2 = self.abs
+ factors = []
+ while self2 != 1
+ factors << (2..self2).detect { |i| i.prime? and self2 % i == 0 }
+ self2 = self2 / factors.last
+ end
+ factors
+ end
+end
+
+class Integer
+ def harmonic
+ sum = Rational(0)
+ (1..self).each { |i| sum = sum + Rational(1, i) }
+ sum
+ end
+end
+
+class Integer
+ def digits
+ array_for_digits = []
+ self2 = self.abs
+ while self2 != 0
+ array_for_digits << self2 % 10
+ self2 = self2 / 10
+ end
+ array_for_digits.reverse
+ end
+end
+
+class Array
+ def average
+ sum = Float(0)
+ self.each { |i| sum = sum + i }
+ return sum / self.length
+ end
+end
+
+class Array
+ def drop_every(n)
+ arraycopy = self
+ k = n
+ while n <= arraycopy.length
+ arraycopy.delete_at(n-1)
+ n = n + k - 1
+ end
+ arraycopy
+ end
+end
+
+class Array
+ def frequencies
+ ar = self.uniq
+ hash = {}
+ (0..(ar.length - 1)).each do |i|
+ hash = hash.merge({ar[i] => self.count(ar[i])})
+ end
+ hash
+ end
+end
+
+class Array
+ def combine_with(arr)
+ new_array = []
+ i = 0
+ while ( self[i] or arr[i] )
+ new_array << self[i] << arr[i]
+ i = i + 1
+ end
+ new_array.compact
+ end
+end