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

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

Към профила на Христо Аврамов

Резултати

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

Код

def degree_of (dividend, divisor)
how_many_divides=[]
while dividend>1 and dividend % divisor == 0 do
how_many_divides += 1
dividend /= divisor
end
how_many_divides
end
class Integer
#is the number prime
def prime?
current_divisior=2
if self <= 1 then return false end
while current_divisior <= Math.sqrt(self)
if self % current_divisior==0 then return false end
current_divisior+=1
end
true
end
def next_prime
copy_self = self + 1
copy_self += 1 until copy_self.prime?
copy_self
end
#return array with all prime dividers of
def prime_factors
copy_self,i = self.abs,2
prime_dividers = []
#6 lines...sure...
while copy_self > 1 do while copy_self % i == 0 do prime_dividers.push(i)
copy_self /= i
end
i=i.next_prime
end
prime_dividers
end
#n-th harmonic
def harmonic
sum = Rational(0, 1)
i=1
while i <= self do
sum += Rational(1, i)
i +=1
end
sum
end
#return an array with the digits of the number
def digits
copy_self = self.abs
all_digits = []
while copy_self > 0 do
all_digits.push copy_self%10
copy_self /= 10
end
all_digits.reverse
end
end
class Array
def frequencies
# all should be all_element_with_their_friequencies = {}
all = {}
#elements should be elements_of_the_array
self.each do |elements|
if(all[elements]) then all[elements] += 1 else all[elements] = 1 end
end
all
end
def average
sum = 0.0
self.each { |element_of_self| sum += element_of_self }
sum/self.length
end
def drop_every(n)
without_every_nth =[]
i = 0
while i < self.length do
without_every_nth.push self[i] unless i % n == n-1
i += 1
end
without_every_nth
end
def combine_with (other)
#6 lines... ok
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-12i71ed/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 combines two arrays by alternatingly taking elements
     Failure/Error: [].combine_with([]).should                          eq []
       
       expected: []
            got: nil
       
       (compared using ==)
     # /tmp/d20131023-4395-12i71ed/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.01866 seconds
14 examples, 2 failures

Failed examples:

rspec /tmp/d20131023-4395-12i71ed/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-12i71ed/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Христо обнови решението на 16.10.2013 03:00 (преди около 11 години)

+ def degree_of (dividend, divisor)
+ how_many_divides=[]
+ while dividend>1 and dividend % divisor == 0 do
+ how_many_divides += 1
+ dividend /= divisor
+ end
+ how_many_divides
+ end
+
+
+class Integer
+ #is the number prime
+ def prime?
+ current_divisior=2
+
+ if self <= 1 then return false end
+ while current_divisior <= Math.sqrt(self)
+ if self % current_divisior==0 then return false end
+ current_divisior+=1
+ end
+ true
+ end
+
+ def next_prime
+ copy_self = self + 1
+ copy_self += 1 until copy_self.prime?
+ copy_self
+ end
+ #return array with all prime dividers of
+ def prime_factors
+ copy_self,i = self.abs,2
+ prime_dividers = []
+ #6 lines...sure...
+ while copy_self > 1 do while copy_self % i == 0 do prime_dividers.push(i)
+ copy_self /= i
+ end
+ i=i.next_prime
+ end
+ prime_dividers
+ end
+
+ #n-th harmonic
+ def harmonic
+ sum = Rational(0, 1)
+ i=1
+ while i <= self do
+ sum += Rational(1, i)
+ i +=1
+ end
+ sum
+ end
+ #return an array with the digits of the number
+ def digits
+ copy_self = self.abs
+ all_digits = []
+
+ while copy_self > 0 do
+ all_digits.push copy_self%10
+ copy_self /= 10
+ end
+ all_digits.reverse
+ end
+end
+
+class Array
+
+ def frequencies
+ # all should be all_element_with_their_friequencies = {}
+ all = {}
+ #elements should be elements_of_the_array
+ self.each do |elements|
+ if(all[elements]) then all[elements] += 1 else all[elements] = 1 end
+ end
+ all
+ end
+
+ def average
+ sum = 0.0
+
+ self.each { |element_of_self| sum += element_of_self }
+ sum/self.length
+ end
+
+ def drop_every(n)
+ without_every_nth =[]
+ i = 0
+
+ while i < self.length do
+ without_every_nth.push self[i] unless i % n == n-1
+ i += 1
+ end
+ without_every_nth
+ end
+ def combine_with (other)
+ #6 lines... ok
+ end
+
+end