Решение на Първа задача от Валентина Динкова

Обратно към всички решения

Към профила на Валентина Динкова

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 17 успешни тест(а)
  • 0 неуспешни тест(а)

Код

class Array
def to_hash
self.inject({}){|h,x| k,v = x ; h[k]=v ; h }
end
def index_by
result={}
self.each do |item|
result[yield(item)] = item
end
result
end
def subarray_count(subarray)
count=0
(0..self.size).each do |i|
next if self[i ... i+subarray.size] != subarray
count += 1
end
count
end
def occurences_count
result = {}
self.each do |item|
if result.has_key?(item)
result[item] += 1
else
result[item] = 1
end
end
result.default = 0
result
end
end

Лог от изпълнението

.................

Finished in 0.01402 seconds
17 examples, 0 failures

История (1 версия и 0 коментара)

Валентина обнови решението на 23.10.2011 22:05 (преди над 12 години)

+class Array
+
+ def to_hash
+ self.inject({}){|h,x| k,v = x ; h[k]=v ; h }
+ end
+
+ def index_by
+ result={}
+ self.each do |item|
+ result[yield(item)] = item
+ end
+ result
+ end
+
+ def subarray_count(subarray)
+ count=0
+ (0..self.size).each do |i|
+ next if self[i ... i+subarray.size] != subarray
+ count += 1
+ end
+ count
+ end
+
+ def occurences_count
+ result = {}
+ self.each do |item|
+ if result.has_key?(item)
+ result[item] += 1
+ else
+ result[item] = 1
+ end
+ end
+ result.default = 0
+ result
+ end
+
+end