Ивайло обнови решението на 23.10.2011 21:49 (преди около 13 години)
+class Array
+ def to_hash
+ result = Hash.new
+ for i in 0...self.size
+ result[self[i][0]] = self[i][1]
+ end
+ result
+ end
+
+ def index_by
+ result = Hash.new
+ for i in 0...self.size
+ result[yield self[i]] = self[i]
+ end
+ result
+ end
+
+ def subarray_count subarray
+ result = 0
+ if subarray.size <= self.size
+ for i in 0...self.size
+ if self[i, subarray.size] == subarray
+ result += 1
+ end
+ end
+ end
+ result
+ end
+
+ def occurences_count
+ result = Hash.new 0
+ for i in 0...self.size
+ result[self[i]] = if !result[self[i]] then 1 else 1 + result[self[i]] end
+ end
+ result
+ end
+end