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

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

Към профила на Давид Петров

Резултати

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

Код

class Integer
def prime?
2.upto(Math.sqrt self.abs ).all? { |x| self.remainder(x).nonzero? }
end
def prime_factors n=self.abs, divisor=2, factors=[]
if n==1
factors
elsif n.remainder(divisor).zero?
prime_factors n/divisor, divisor, factors.push(divisor)
else
prime_factors n, divisor+1, factors
end
end
def harmonic
sum=Rational(0,1)
1.upto(self).each {|x| sum+=Rational(1,x)}
sum
end
def digits
digits_array=[]
self.abs.to_s.each_codepoint {|x| digits_array.push(x-48)}
digits_array
end
end
class Array
def frequencies
frequency={}
self.each {|x| frequency.has_key?(x) ? frequency[x]+=1 : frequency[x]=1}
frequency
end
def average
self.inject {|a,b| a+b}/self.length.to_f
end
def drop_every(n)
reduced=[]
self.each_index {|x| reduced.push(self[x]) if (x+1).remainder(n).nonzero?}
reduced
end
def combine_with(arr)
combined_array=Array.new(self.length+arr.length)
self.each_index {|x| combined_array[2*x]=self[x]}
arr.each_index {|x| combined_array[2*x+1]=arr[x]}
combined_array.compact
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: -13.prime?.should eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-1f6wyrh/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: [: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-1f6wyrh/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.02212 seconds
14 examples, 2 failures

Failed examples:

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

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

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

+class Integer
+ def prime?
+ 2.upto(Math.sqrt self.abs ).all? { |x| self.remainder(x).nonzero? }
+ end
+
+ def prime_factors n=self.abs, divisor=2, factors=[]
+ if n==1
+ factors
+ elsif n.remainder(divisor).zero?
+ prime_factors n/divisor, divisor, factors.push(divisor)
+ else
+ prime_factors n, divisor+1, factors
+ end
+ end
+
+ def harmonic
+ sum=Rational(0,1)
+ 1.upto(self).each {|x| sum+=Rational(1,x)}
+ sum
+ end
+
+ def digits
+ digits_array=[]
+ self.abs.to_s.each_codepoint {|x| digits_array.push(x-48)}
+ digits_array
+ end
+
+end
+
+class Array
+ def frequencies
+ frequency={}
+ self.each {|x| frequency.has_key?(x) ? frequency[x]+=1 : frequency[x]=1}
+ frequency
+ end
+
+ def average
+ self.inject {|a,b| a+b}/self.length.to_f
+ end
+
+ def drop_every(n)
+ reduced=[]
+ self.each_index {|x| reduced.push(self[x]) if (x+1).remainder(n).nonzero?}
+ reduced
+ end
+
+ def combine_with(arr)
+ combined_array=Array.new(self.length+arr.length)
+ self.each_index {|x| combined_array[2*x]=self[x]}
+ arr.each_index {|x| combined_array[2*x+1]=arr[x]}
+ combined_array.compact
+ end
+
+end