Михаил обнови решението на 22.10.2011 16:54 (преди около 13 години)
+class Array
+ def to_hash ()
+ length = self.length()
+ hash = Hash.new
+ for i in 0..length-1
+ key = self[i][0]
+ hash[key] = self[i][1]
+ end
+ p hash
+ end
+
+ def index_by ()
+ hash = Hash.new
+ self.each do |n|
+ hash[yield n] = n
+ end
+ p hash
+ end
+
+ def subarray_count (subarray)
+ length = 0
+ counter = 0
+ self.each do |n|
+ temp_array = Array.new
+ break if length == self.length() - subarray.length() + 1
+ for i in length..length + subarray.length() - 1
+ temp_array << self[i]
+ end
+ if temp_array == subarray
+ counter += 1
+ end
+ length += 1
+ end
+ p counter
+ end
+
+ def occurences_count ()
+ hash = Hash.new
+ hash.default = 0
+ self.each do |n|
+ if hash.has_key?(n)
+ hash[n] += 1
+ else
+ hash[n] = 1
+ end
+ end
+ hash
+ end
+end