Явор обнови решението на 24.10.2011 16:19 (преди около 13 години)
+#Prechat li komentarite da znam dali da slagam drugiq put :)
+class Array
+ def to_hash()
+ temp={} # syzdavame prazen hesh koito shte napulnim s elementite na masiva 'self'
+ j=0
+ while j<=self.length-1
+ i=0
+ while i<self.length-1
+ temp[self[j][i]]=self[j][i+1] #kluchut e vseki element[j][i], a stoinostta vseki element[j][i+1]
+ i+=2
+ end
+ j+=1
+ end
+ return temp # vrushtame polucheniq hesh
+ end
+#--------------------------------------------------------------
+ def index_by
+ temp={} # syzdavame prazen hesh koito shte napulnim s elementite na masiva 'self'
+ i=0
+ while i<self.length
+ current=self[i] #vzimame i-tiq element ot masiva 'self'
+ temp[yield current]=self[i] # kluchyt e i-tiq element preraboten ot bloka, a stoinostta e samiqt i-ti element ot masiva 'self'
+ i+=1
+ end
+ return temp # vrushtame polucheniq hesh
+ end
+#--------------------------------------------------------------
+ def subarray_count(subarray)
+ i=0;j=0;sum=0;
+ while j<self.length
+ temp=[]
+ i=j
+ while i<subarray.length+j
+ temp+=[self[i]] #dobavqme subarray.length na broi elementi ot masiva 'self' v masiva 'temp' kato zapochvame ot index j
+ i+=1
+ end
+ if(temp==subarray) #ako masiva 'a' e syshtiq kato 'subarray' uvelichavame broq sreshtaniq s 1
+ sum+=1
+ end
+ j+=1 # premestvame indexa j s 1
+ end
+ return sum # vrushtame polucheniq broi sreshtaniq
+ end
+#--------------------------------------------------------------
+ def occurences_count
+ temp={}# syzdavame prazen hesh koito shte napulnim s elementite na masiva 'self'
+ i=0
+ while i<self.length
+ current=self.subarray_count([self[i]]) #vzimame broq sreshtaniq na i-tiq element v 'self' s metoda subarray_count i go zapazvame v current
+ temp[self[i]]=current #kluchyt e i-tiq element na 'self', a stoinostta e current
+ i+=1
+ end
+ return temp # vrushtame polucheniq hesh
+ end
+end
+
+#---------------------------------------------------------------
+#izprobvane na metodite
+puts 'Metoda #to_hash:'
+puts [[:one, 1], [:two, 2]].to_hash
+puts [[1, 2], [3, 4]].to_hash
+puts [[1, 2], [1, 3]].to_hash
+puts [].to_hash
+
+puts 'Metoda #index_by'
+puts ['John Coltrane', 'Miles Davis'].index_by { |name| name.split(' ').last }
+puts %w[foo larodi bar].index_by { |s| s.length }
+
+puts 'Metoda #subarray_count'
+puts [1, 2, 3, 2, 3, 1].subarray_count([2, 3])
+puts [1, 2, 2, 2, 2, 1].subarray_count([2, 2])
+puts [1, 1, 2, 2, 1, 1, 1].subarray_count([1, 1])
+
+puts 'Metoda #occurences_count'
+puts [:foo, :bar, :foo].occurences_count
+puts %w[a a b c b a].occurences_count