Любомир обнови решението на 16.10.2013 01:00 (преди около 11 години)
+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