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

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

Към профила на Йордан Дикманов

Резултати

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

Код

class Integer
def prime?
max=Math.sqrt((self).abs).floor
max.downto(2) do |n| if self % n == 0 then return false end end
return true
end
def prime_factors
array_prime , new_numb , delitel = Array.new , self.abs , 2
while new_numb > 1
if delitel.prime? and new_numb % delitel==0 then array_prime << delitel
new_numb /= delitel
else delitel = delitel.next
end
end
return array_prime.sort
end
def harmonic
sum=Rational(1,1)
2.upto(self) do |n| sum += Rational(1.0,n)
end
return sum
end
def digits
array_digits , length=Array.new , Math.log(self.abs,10).ceil+1
str = self.abs.to_s
length.times do |i|
if str[i] != nil then array_digits <<str[i].to_i
else
return array_digits end
end
end
end
class Array
def frequencies
br, h=0 , Hash.new
self.each do |i| if h[i] == nil then h[i]=1 else h[i]=h[i].to_i.next end
end
return h
end
def average
sum=0.0
self.each do |i| sum += i
end
if self.length !=0 then return sum/self.length else return "Masiva e prazen"
end
end
def drop_every( x )
new_array=Array.new
1.upto(self.length) do |i| if i % x !=0 then new_array << self[i-1] end
end
return new_array
end
def combine_with( other )
maxelem , com_arr=[ self.length , other.length ].max,Array.new
0.upto( maxelem ) do |i| if self[i] != nil then com_arr << self[i] end
if other[i] != nil then com_arr << other[i] end
end
return com_arr
end
end

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

F...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-ir5trd/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) Integer#digits constructs an array containing the digits of a number
     Failure/Error: 0.digits.should      eq [0]
     FloatDomainError:
       -Infinity
     # /tmp/d20131023-4395-ir5trd/solution.rb:27:in `ceil'
     # /tmp/d20131023-4395-ir5trd/solution.rb:27:in `digits'
     # /tmp/d20131023-4395-ir5trd/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#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-ir5trd/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.01921 seconds
14 examples, 3 failures

Failed examples:

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

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

Йордан обнови решението на 14.10.2013 22:06 (преди около 11 години)

+class Integer
+ def prime?
+ max=Math.sqrt((self).abs).floor
+ max.downto(2) do |n| if self % n == 0 then return false end end
+ return true
+ end
+
+ def prime_factors
+ array_prime , new_numb , delitel = Array.new , self.abs , 2
+ while new_numb > 1
+ if delitel.prime? and new_numb % delitel==0 then array_prime << delitel
+ new_numb /= delitel
+ else delitel = delitel.next
+ end
+ end
+ return array_prime.sort
+ end
+
+ def harmonic
+ sum=Rational(1,1)
+ 2.upto(self) do |n| sum += Rational(1.0,n)
+ end
+ return sum
+ end
+
+ def digits
+ array_digits , length=Array.new , Math.log(self.abs,10).ceil+1
+ str = self.abs.to_s
+ length.times do |i|
+ if str[i] != nil then array_digits <<str[i].to_i
+ else
+ return array_digits end
+ end
+ end
+end
+class Array
+ def frequencies
+ br, h=0 , Hash.new
+ self.each do |i| if h[i] == nil then h[i]=1 else h[i]=h[i].to_i.next end
+ end
+ return h
+ end
+ def average
+ sum=0.0
+ self.each do |i| sum += i
+ end
+ if self.length !=0 then return sum/self.length else return "Masiva e prazen"
+ end
+ end
+ def drop_every( x )
+ new_array=Array.new
+ 1.upto(self.length) do |i| if i % x !=0 then new_array << self[i-1] end
+ end
+ return new_array
+ end
+ def combine_with( other )
+ maxelem , com_arr=[ self.length , other.length ].max,Array.new
+ 0.upto( maxelem ) do |i| if self[i] != nil then com_arr << self[i] end
+ if other[i] != nil then com_arr << other[i] end
+ end
+ return com_arr
+ end
+end