Николай обнови решението на 13.10.2012 15:33 (преди около 12 години)
+class Integer
+ def divides_of? number
+ self % number == 0
+ end
+
+ def prime?
+ self > 1 and (2..Math.sqrt(self)).all? { |x| not divides_of? x }
+ end
+
+ def prime_divisors
+ (2..abs).select { |x| divides_of? x and x.prime? }
+ end
+end
+
+class Range
+ def fizzbuzz
+ collect do |x|
+ if x.kind_of? Integer and (x.divides_of? 3 or x.divides_of? 5)
+ if x.divides_of? 3 and x.divides_of? 5
+ :fizzbuzz
+ elsif x.divides_of? 3
+ :fizz
+ else
+ :buzz
+ end
+ else
+ x
+ end
+ end
+ end
+end
+
+class Hash
+ def group_values
+ result = Hash.new { |hash, key| hash[key] = [] }
+ each { |key, value| result[value] << key }
+ result
+ end
+end
+
+class Array
+ def densities
+ densities = Hash.new { |hash, key| hash[key] = count(key)}
+ Array collect { |x| densities[x] }
+ end
+end