Решение на Първа задача от Любомир Георгиев

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

Към профила на Любомир Георгиев

Резултати

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

Код

class Integer
def prime?
return false if self < 2
end_range = self
(2...end_range).each do |number|
return false if (end_range % number) == 0
end
true
end
def prime_factors
number = self.abs
return [] if number == 1 or number == 0
divisor = (2..number).find { |x| number % x == 0 }
[divisor] + (number / divisor).prime_factors
end
def harmonic
number = self
return nil if number < 1
return 1 if number == 1
Rational(1, number) + (number-1).harmonic
end
def digits
number = self.abs
digits_array = []
until number == 0 do
digits_array.unshift(number % 10)
number /= 10
end
digits_array
end
end
class Array
#too long names here. some whitespace reduced : /
def frequencies
given_array = self
frequencies_hash = {}
given_array.each do |key|
frequencies_hash[key]=frequencies_hash[key] ? frequencies_hash[key]+1 : 1
end
frequencies_hash
end
def average
given_array = self
array_length = given_array.length
return nil if array_length == 0
given_array.inject(:+) / array_length.to_f
end
def drop_every(n)
cleaned_array = self
position = n - 1
while position < cleaned_array.length
cleaned_array.delete_at(position)
position += n - 1
end
cleaned_array
end
def combine_with(array_two)
array_one = self
max_lenght = [array_one.length, array_two.length].max
combined_array = []
(0...max_lenght).each do |i|
combined_array.push_if_exist!(array_one[i]).push_if_exist!(array_two[i])
end
combined_array
end
def push_if_exist!(element)
self.push(element) if not element.nil?
end
def push_if_exist(element)
dup.push_if_exist!(element)
end
end

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

....F......FF.

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-13qc7tm/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#drop_every doesn't change the array
     Failure/Error: expect { array.drop_every(3) }.to_not change { array }
       result should not have changed, but did change from [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] to [1, 2, 4, 5, 7, 8, 10]
     # /tmp/d20131023-4395-13qc7tm/spec.rb:98: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: [].combine_with([1, 2, 3]).should                   eq [1, 2, 3]
     NoMethodError:
       undefined method `push_if_exist!' for nil:NilClass
     # /tmp/d20131023-4395-13qc7tm/solution.rb:72:in `block in combine_with'
     # /tmp/d20131023-4395-13qc7tm/solution.rb:71:in `each'
     # /tmp/d20131023-4395-13qc7tm/solution.rb:71:in `combine_with'
     # /tmp/d20131023-4395-13qc7tm/spec.rb:105: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.01909 seconds
14 examples, 3 failures

Failed examples:

rspec /tmp/d20131023-4395-13qc7tm/spec.rb:43 # Integer#digits constructs an array containing the digits of a number
rspec /tmp/d20131023-4395-13qc7tm/spec.rb:96 # Array#drop_every doesn't change the array
rspec /tmp/d20131023-4395-13qc7tm/spec.rb:103 # Array#combine_with combines two arrays by alternatingly taking elements

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

Любомир обнови решението на 16.10.2013 01:00 (преди над 10 години)

+class Integer
+
+ def prime?
+ return false if self < 2
+ end_range = self
+ (2...end_range).each do |number|
+ return false if (end_range % number) == 0
+ end
+ true
+ end
+
+ def prime_factors
+ number = self.abs
+ return [] if number == 1 or number == 0
+ divisor = (2..number).find { |x| number % x == 0 }
+ [divisor] + (number / divisor).prime_factors
+ end
+
+ def harmonic
+ number = self
+ return nil if number < 1
+ return 1 if number == 1
+ Rational(1, number) + (number-1).harmonic
+ end
+
+ def digits
+ number = self.abs
+ digits_array = []
+ until number == 0 do
+ digits_array.unshift(number % 10)
+ number /= 10
+ end
+ digits_array
+ end
+
+end
+
+class Array
+
+ #too long names here. some whitespace reduced : /
+ def frequencies
+ given_array = self
+ frequencies_hash = {}
+ given_array.each do |key|
+ frequencies_hash[key]=frequencies_hash[key] ? frequencies_hash[key]+1 : 1
+ end
+ frequencies_hash
+ end
+
+ def average
+ given_array = self
+ array_length = given_array.length
+ return nil if array_length == 0
+ given_array.inject(:+) / array_length.to_f
+ end
+
+ def drop_every(n)
+ cleaned_array = self
+ position = n - 1
+ while position < cleaned_array.length
+ cleaned_array.delete_at(position)
+ position += n - 1
+ end
+ cleaned_array
+ end
+
+ def combine_with(array_two)
+ array_one = self
+ max_lenght = [array_one.length, array_two.length].max
+ combined_array = []
+ (0...max_lenght).each do |i|
+ combined_array.push_if_exist!(array_one[i]).push_if_exist!(array_two[i])
+ end
+ combined_array
+ end
+
+ def push_if_exist!(element)
+ self.push(element) if not element.nil?
+ end
+
+ def push_if_exist(element)
+ dup.push_if_exist!(element)
+ end
+
+end