Димитър обнови решението на 23.10.2011 14:41 (преди около 13 години)
+class Array
+
+ def to_hash
+
+ the_hash = Hash.new
+
+ self.each do |n|
+
+ if(n.class != Array && n.size != 2) then continue end
+
+ the_hash[n[0]]=n[1]
+
+ end # self.each
+
+ return the_hash
+ end
+
+
+ def index_by &block
+
+ result = Hash.new
+
+ self.each do |n|
+ result[block.call(n)] = n
+ end
+
+ return result
+
+ end
+
+
+ def subarray_count arr
+ counter = 0
+ i = 0 # interator
+ while i<self.size
+ subarray = self[i..(i+arr.size)]
+ if subarray == [] then
+ i+=1
+ next
+ end
+
+ ii =0
+ maching = 1
+ while ii < arr.size
+
+ if arr[ii] != subarray[ii] then
+ maching = 0
+ break;
+ end
+ ii+=1
+ end
+ if maching ==1 then counter +=1 end
+
+ i+=1
+ end
+ return counter
+ end
+
+ def occurences_count
+
+ result_hash = Hash.new
+ self.each do |n|
+ if result_hash.has_key?(n) then
+ result_hash[n] +=1
+ else
+ result_hash[n] = 1
+ end
+ end
+ return result_hash
+ end
+
+end