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

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

Към профила на Добромир Младенов

Резултати

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

Код

class Integer
def prime?
if self < 1
return false
end
(2...self).step(1) do |n|
if self.remainder(n) == 0
return false
end
end
return true
end
end
class Integer
def digits
numb_digits = []
number = self.abs
while number != 0
numb_digits.insert(0, number.remainder(10))
number = number / 10
end
return numb_digits
end
end
class Array
def average
avrg = 0.0
self.each do |n|
avrg = avrg + n
end
return avrg / self.size
end
end
class Integer
def prime_factors
if self.abs < 2
return []
end
(2..self.abs).step(1) do |n|
if self.remainder(n) == 0 and n.prime?
return [n] + (self / n).prime_factors
end
end
return []
end
end
class Integer
def harmonic
sum = Rational(0)
(1..self).step(1) do |k|
sum = sum + Rational(1, k)
end
return sum
end
end
class Array
def frequencies
frequence_hash = {}
self.each do |i|
element = i
frequence_hash[element] = frequence_hash.fetch(element, 0) + 1
end
return frequence_hash
end
end
class Array
def drop_every(n)
not_dropped = []
index = 0
while index < self.size
if (index + 1) % n != 0 or index == 0 then not_dropped << self[index] end
index = index + 1
end
return not_dropped
end
end
class Array
def combine_with(other)
merge = self + other
mix = []
merge.each_index do |i|
if i < self.size then mix = mix + [ merge[i] ] end
if i < other.size then mix = mix + [ merge[i + self.size] ] end
end
return mix
end
end

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

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

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-17sl6l5/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-17sl6l5/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)>'

Finished in 0.01886 seconds
14 examples, 2 failures

Failed examples:

rspec /tmp/d20131023-4395-17sl6l5/spec.rb:2 # Integer#prime? checks if a number is prime
rspec /tmp/d20131023-4395-17sl6l5/spec.rb:43 # Integer#digits constructs an array containing the digits of a number

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

Добромир обнови решението на 15.10.2013 21:41 (преди около 11 години)

+class Integer
+ def prime?
+ if self < 1
+ return false
+ end
+ (2...self).step(1) do |n|
+ if self.remainder(n) == 0
+ return false
+ end
+ end
+ return true
+ end
+end
+
+class Integer
+ def digits
+ numb_digits = []
+ number = self.abs
+ while number != 0
+ numb_digits.insert(0, number.remainder(10))
+ number = number / 10
+ end
+ return numb_digits
+ end
+end
+
+class Array
+ def average
+ avrg = 0.0
+ self.each do |n|
+ avrg = avrg + n
+ end
+ return avrg / self.size
+ end
+end
+
+class Integer
+ def prime_factors
+ if self.abs < 2
+ return []
+ end
+ (2..self.abs).step(1) do |n|
+ if self.remainder(n) == 0 and n.prime?
+ return [n] + (self / n).prime_factors
+ end
+ end
+ return []
+ end
+end
+
+class Integer
+ def harmonic
+ sum = Rational(0)
+ (1..self).step(1) do |k|
+ sum = sum + Rational(1, k)
+ end
+ return sum
+ end
+end
+
+class Array
+ def frequencies
+ frequence_hash = {}
+ self.each do |i|
+ element = i
+ frequence_hash[element] = frequence_hash.fetch(element, 0) + 1
+ end
+ return frequence_hash
+ end
+end
+
+class Array
+ def drop_every(n)
+ not_dropped = []
+ index = 0
+ while index < self.size
+ if (index + 1) % n != 0 or index == 0 then not_dropped << self[index] end
+ index = index + 1
+ end
+ return not_dropped
+ end
+end
+
+class Array
+ def combine_with(other)
+ merge = self + other
+ mix = []
+ merge.each_index do |i|
+ if i < self.size then mix = mix + [ merge[i] ] end
+ if i < other.size then mix = mix + [ merge[i + self.size] ] end
+ end
+ return mix
+ end
+end