Иван обнови решението на 16.10.2013 15:49 (преди около 11 години)
+class Integer
+ def prime?
+ return false if self < 2
+ square_root = Math.sqrt(self)
+ (2..square_root).each { |n| if self.modulo(n).zero? then return false end }
+ true
+ end
+
+ def harmonic
+ harmonic_sum = 0
+ (1..self).each { |n| harmonic_sum += Rational(1, n) }
+ harmonic_sum
+ end
+
+ def digits
+ number_to_array = self.abs.to_s.split('')
+ number_to_array.map do |dig|
+ dig.to_i
+ end
+ end
+end
+
+class Array
+ def drop_every(n)
+ result_drop = []
+ tll = ->(i,n) {|i, n| (i+1).modulo(n).zero? } #too long line
+ (0...self.size).each { |i| result_drop << self[i] unless tll(i,n) }
+ result_drop
+ end
+
+ def combine_with(other)
+ combination = []
+ length_max = if self.size > other.size then self.size else other.size end
+ (0...length_max).each { |i| combination << self[i] << other[i] }
+ combination.compact
+ end
+
+ def average
+ sum = 0.0
+ self.each {|n| sum += n }
+ return sum / self.size
+ end
+
+ def frequencies
+ inject(Hash.new(0)) { |h,e| h[e] += 1 and h }.inject({}) {
+ |r, e| r[e.first] = e.last and r }
+ end
+end