Радина обнови решението на 16.10.2013 03:31 (преди около 11 години)
+class Integer
+ def prime?
+ return false if self < 2
+ 2.upto(self / 2) do |x| return false if self % x == 0 end
+ true
+ end
+ def prime_factors
+ int = self.abs
+ return [int] if int.prime?
+ factors = []
+ 2.upto(int / 2) do |x| (factors.push x and int /= x) while int % x == 0 end
+ factors
+ end
+ def harmonic
+ sum = 1/1r
+ return sum if self == 1
+ 2.upto(self) do |x| sum += Rational(1) / x end
+ sum
+ end
+ def digits
+ int = self.abs
+ return [int] if int < 10
+ digits_ls = []
+ (digits_ls.push (int % 10) and int /= 10) while int > 0
+ digits_ls.reverse
+ end
+ end
+class Array
+ def frequencies
+ freq_ls = {}
+ 0.upto(self.size - 1) do |x|
+ n = self.select{ |i| i == self[x] } and freq_ls[self[x]] = n.size end
+ freq_ls
+ end
+ def average
+ sum = 0.0
+ 0.upto(self.size) do |x| val = self[x] and sum += val end
+ sum / self.size
+ end
+ def drop_every(n)
+ left_elements = []
+ 0.upto(self.size - 1) do |x|
+ left_elements.push self[x] if (x + 1) % n != 0 end
+ left_elements
+ end
+ def combine_with(other)
+ return self if other == []
+ return other if self == []
+ comb_ls = []
+ self.size > other.size ? max_size = self.size : max_size = other.size
+ 0.upto(max_size) do |x| comb_ls.push self[x] and comb_ls.push other[x] end
+ comb_ls.select{ |x| x !=nil }
+ end
+ end