Добромир обнови решението на 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