Решение на Първа задача от Борис Минев

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

Към профила на Борис Минев

Резултати

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

Код

class Array
def to_hash()
self.inject({}) { |hash, x| hash[x[0]] = x[1]; hash }
end
def index_by(&block)
self.inject({}) { |hash, x| hash[block.(x)] = x; hash }
end
def subarray_count(subarr)
(0...self.length-subarr.length+1).to_a.inject([]) { |arr, x| arr<<self[x...x+subarr.length]; arr }.count(subarr)
end
def occurences_count()
self.inject(Hash.new(0)) { |hash, x| hash[x] = self.count(x); hash }
end
end

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

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

Finished in 0.01396 seconds
17 examples, 0 failures

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

Борис обнови решението на 24.10.2011 15:07 (преди над 12 години)

+class Array
+ def to_hash()
+ self.inject({}) { |hash, x| hash[x[0]] = x[1]; hash }
+ end
+ def index_by(&block)
+ self.inject({}) { |hash, x| hash[block.(x)] = x; hash }
+ end
+ def subarray_count(subarr)
+ (0...self.length-subarr.length+1).to_a.inject([]) { |arr, x| arr<<self[x...x+subarr.length]; arr }.count(subarr)
+ end
+ def occurences_count()
+ self.inject(Hash.new(0)) { |hash, x| hash[x] = self.count(x); hash }
+ end
+end