Решение на Първа задача от Ангел Цанев

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

Към профила на Ангел Цанев

Резултати

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

Код

class Integer
def prime?
(self == self.abs) and 2.upto(self - 1).all? { |i| remainder(i).nonzero? }
end
def prime_factors
prime_factors_array = []
current_number = self.abs
while (current_number > 1)
prime_factors_array << current_number.lowest_prime_devisor
current_number /= current_number.lowest_prime_devisor
end
return prime_factors_array
end
def lowest_prime_devisor
given_number = self
2.upto(given_number).each do |likely_divisor|
if likely_divisor.prime? and given_number.remainder(likely_divisor).zero?
return likely_divisor
end
end
end
def harmonic
if self == 1
Rational(1,1)
else
1.upto(self).inject { |sum, current| sum += Rational(1,current) }
end
end
def digits
given_digit = self.abs
digits_array = []
while(given_digit > 0)
digits_array.insert(0, given_digit.remainder(10))
given_digit /= 10
end
return digits_array
end
end
class Array
def frequencies
hash = Hash.new(0)
self.each { |i| hash[i] += 1 }
#hash.each {|key, value| } puts "#{key} appears #{value} times"}
return hash
end
def average
if self == []
return 0.0
else
((self.inject { |sum, current| sum += current } ).to_f / self.size)
end
end
def drop_every(step)
new_array = self.dup
(self.size / step).downto(1).each { |i| new_array.delete_at(i * step - 1) }
return new_array
end
def combine_with(second_array = [])
array_to_be_returned = []
upper_limit = ([self.size, second_array.size].max - 1)
0.upto(upper_limit).each do |i|
array_to_be_returned << self[i] if self[i]
array_to_be_returned << second_array[i] if second_array[i]
end
return array_to_be_returned
end
end

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

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

Failures:

  1) Integer#prime? checks if a number is prime
     Failure/Error: 0.prime?.should   eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131023-4395-1kamxp6/spec.rb:4: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-1kamxp6/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-1kamxp6/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.02422 seconds
14 examples, 3 failures

Failed examples:

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

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

Ангел обнови решението на 15.10.2013 11:17 (преди около 11 години)

+class Integer
+
+ def prime?
+ (self == self.abs) and 2.upto(self - 1).all? { |i| remainder(i).nonzero? }
+ end
+
+ def prime_factors
+ prime_factors_array = []
+ current_number = self.abs
+
+ while (current_number > 1)
+ prime_factors_array << current_number.lowest_prime_devisor
+ current_number /= current_number.lowest_prime_devisor
+ end
+
+ return prime_factors_array
+ end
+
+ def lowest_prime_devisor
+ given_number = self
+
+ 2.upto(given_number).each do |likely_divisor|
+ if likely_divisor.prime? and given_number.remainder(likely_divisor).zero?
+ return likely_divisor
+ end
+ end
+ end
+
+ def harmonic
+ if self == 1
+ Rational(1,1)
+ else
+ 1.upto(self).inject { |sum, current| sum += Rational(1,current) }
+ end
+ end
+
+ def digits
+ given_digit = self.abs
+ digits_array = []
+
+ while(given_digit > 0)
+ digits_array.insert(0, given_digit.remainder(10))
+ given_digit /= 10
+ end
+
+ return digits_array
+ end
+end
+
+class Array
+
+ def frequencies
+ hash = Hash.new(0)
+
+ self.each { |i| hash[i] += 1 }
+ #hash.each {|key, value| } puts "#{key} appears #{value} times"}
+
+ return hash
+ end
+
+ def average
+ if self == []
+ return 0.0
+ else
+ ((self.inject { |sum, current| sum += current } ).to_f / self.size)
+ end
+ end
+
+ def drop_every(step)
+ new_array = self.dup
+
+ (self.size / step).downto(1).each { |i| new_array.delete_at(i * step - 1) }
+
+ return new_array
+ end
+
+ def combine_with(second_array = [])
+ array_to_be_returned = []
+ upper_limit = ([self.size, second_array.size].max - 1)
+ 0.upto(upper_limit).each do |i|
+ array_to_be_returned << self[i] if self[i]
+ array_to_be_returned << second_array[i] if second_array[i]
+ end
+ return array_to_be_returned
+ end
+
+end